Monday 24th, November 2014
This article is at the time of Kotlin 0.9.206, and some parts are likely to be quickly out of date.
Don't be overwhelmed by the number of steps, most of these are common sense, I'm just trying to be thorough.
This article is to get a Hello World, with unit tests, up and running in Kotlin for the JVM.
This article was written using IntelliJ IDEA 14.
Note: If you are upgrading IDEA and previously had the plugin, be sure to uninstall the Kotlin plugin and get the latest version.
File > Settings > Plugins > Browse Repositories
File > New Project
Select Kotlin, Kotlin - JVM
You should have an empty project now that looks something like this:
Inside the lib folder you should have a kotlin-runtime.jar and a kotlin-runtime-sources.jar
Right click your src directory, New > Kotlin File
Create the file with the name Hello
We need a package-level main function. This is our entry point.
Pro tip: You can generate this method just by typing "main" then hitting tab.
Inside Hello.kt, right click in the editor, and select "Debug '_DefaultPackage'"
We should now see a "Hello World" in the console.
Now I am not a Test Driven Development nut. I won't give you my opinions on what are the best frameworks or styles of testing to use, but I do like to write my unit tests alongside my application. I do this mainly because it simply means faster iteration time. I can focus on a single piece of code instead of an entire application.
Before we create a test, we need something simple to test. In your Hello.kt file let's create a simple class.
Next we need to get a unit testing framework. We can do this easily by adding a maven dependency in our project.
Go to File > Project Structure > Libraries > + New Project Library > From Maven
Then add junit as a dependency.
Next create a folder named "test", and then mark that directory as a test sources root.
Create a new kotlin file in your test folder, choose "class", and call it "HelloTest".
Pay close attention to the imports. The "Test" annotation is what tells the test runner which methods are tests, and you must be sure the org.junit.Test class is imported for that to work.
We can now run the unit test the same way we did our application. Right click either in a specific test method you want to run, or in the class itself to run all tests in that class, and choose "Debug 'helloTest()'"
If all went smoothly, we should see a passing unit test like this:
I hope this helps people get started with Kotlin!