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 org.junit.Test 
    import kotlin.test.assertEquals 
 
    class TestClass { 
      @Test//(1) 
      fun `my first test`() {//(2) 
        assertEquals(3,1+2)//(3) 
      } 
    } 

Have a close look at the preceding program. Each JUnit test case should be defined as a function inside a class. The class that contains the JUnit test functions should only be used for testing purposes and should not serve any other purpose. The test function should be annotated with the @Test annotation, as we did in comment (1). This annotation helps JUnit to detect and execute the tests.

Now, give a cautious look at the line containing comment (2). The function name is `my first test`(). Yes, it contains space within the function name. That is probably the best thing you can get while writing test cases in Kotlin. Kotlin allows you to have functions that have names without spaces, while they aren't good practice while writing codes, they are quite a life saver while writing tests; as you don't need to call the test functions elsewhere, they actually serve as readable test names.

In comment (3), we wrote the actual test. The assertEquals test checks for equality between expected and actual values. The first parameter in this test is the expected value, and the second one is the actual one, which should be equal to the expected one.

If you run the test, you'll get the following output:

If we modify the program and pass 2+3 instead of 1+2 as the actual parameter, then the test would fail and give the following output:

You can also pass a failure message, that would be shown in case of failure, as follows:

    class TestClass { 
      @Test//(1) 
      fun `my first test`() {//(2) 
        assertEquals(3,2+3, "Actual value is not equal to the expected 
one.")//(3) } }

The message would be shown in the error report if the test fails. Have a look at the following output:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.124