TDD is a modern software development process based on creation of automatic test before write code, so modern programming can’t be called in this way without test the software. In this post i want to write something about testing software written in kotlin .
Ok, after this little presentation let’s start to write some code: in the previous post on kotlin the problem was connect to mongo db and write some data. Now i want to complicate this example and the challenge is write a class (data class) and then save it into the database.
In kotlin write a data class is very simple (can find here), my data class for this example is this:
data class Person (val name:String, val surname:String, val age:Int)
Testing this class is very simple, we can test it in the same java way: with junit!
On IntelliJ create test can be done directly from the class:
We can select which methods to test and the IDE create the skeleton for the test class:
I saved this test class in a test folder to keep everything organized. It’s important to configure the test folder for the project:
Now, all it’s configured, and now i can test my class! Run the test class and the result is something like this:
All Green! Great!