Writing JUnit tests in Kotlin
If you've any experience with Java development, you've heard of or most probably worked with JUnit. It is a testing framework, for Java (as well as Kotlin).
Typically unit tests are created in a separate source folder than real source codes, to keep it separated. The standard Maven/Gradle convention uses src/main
for real codes (Java/Kotlin files or classes) and src/test
for test classes. The following screenshot shows the structure for the project we're using in this book:
Before beginning to write test cases we've to add the following Gradle dependencies:
testCompile 'junit:junit:4.12' testCompile "org.mockito:mockito-core:1.9.5" testCompile "org.jetbrains.kotlin:kotlin-test- junit:$kotlin_version"
We've added a dependency to Mockito as well, which we are going to cover soon.
So, we have got everything ready, let's write our first test case. Please refer to the following code:
package com.rivuchk.packtpub.reactivekotlin import...