how test a kotlin class?

flweekendbreakers_t5

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:

Schermata 2016-05-22 alle 21.41.20

Schermata 2016-05-22 alle 21.44.54

We can select which methods to test and the IDE create the skeleton for the test class:

Schermata 2016-05-22 alle 21.48.29

I saved this test class in a test folder to keep everything organized. It’s important to configure the test folder for the project:

Schermata 2016-05-22 alle 21.55.26

Schermata 2016-05-22 alle 21.55.44

Now, all it’s configured, and now i can test my class! Run the test class and the result is something like this:

Schermata 2016-05-22 alle 21.58.28.png

gatsby-is-already-doing-great-at-the-box-office-despite-harsh-reviews      All Green! Great!

 

Advertisements

2 thoughts on “how test a kotlin class?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s