Introduction

When learning a language it is important to know how to write unittests for it. Most modern languages have testing built in. And Kotlin is no exception. Kotlin has the kotlin.test namespace that has all the asserts you need.

Configuration

You will find plenty of examples how to write tests with Kotlin but something was escaping me because my tests would not run.

Let’s take our class.

Java
1
2
3
package demo
 
data class MyClass(val i : Int, val s : String)
package demo

data class MyClass(val i : Int, val s : String)

uhm yeah.

Now lets try our first test.

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
package demo.test
 
import demo.MyClass
import kotlin.test.assertEquals
 
public class tests
{
    test fun IfPropertyIIsSetCorrectly()
    {
        val c1 = MyClass(1,"t")
        assertEquals(1,c1.i)
    }
}
package demo.test

import demo.MyClass
import kotlin.test.assertEquals

public class tests
{
    test fun IfPropertyIIsSetCorrectly()
    {
        val c1 = MyClass(1,"t")
        assertEquals(1,c1.i)
    }
}

I was excpeting the above to run. It doesn’t give a compilation error but the Intellij testrunner doesn’t like it. So I was doing something wrong.

The thing I missed was that it also needs JUnit. You can get maven to download and install it.

And add this line to you import.

Java
1
import org.junit.Test as test
import org.junit.Test as test

Now the intellij testrunner will like them and run them.

More tests.

And here are some more tests.

package demo.test

import demo.MyClass
import kotlin.test.assertEquals
import org.junit.Test as test
import kotlin.test.assertTrue
import kotlin.test.assertFalse

public class tests
{
    test fun IfPropertyIIsSetCorrectly()
    {
        val c1 = MyClass(1,"t")
        assertEquals(1,c1.i)
    }

    test fun IfPropertySIsSetCorrectly()
    {
        val c1 = MyClass(1,"t")
        assertEquals("t",c1.s)
    }

    test fun If2MyClassesWithTheSamePropertiesAreEqual()
    {
        val c1 = MyClass(1,"t")
        val c2 = MyClass(1,"t")
        assertTrue(c1.equals(c2))
    }

    test fun If2MyClassesWithDifferentPropertiesAreNotEqual()
    {
        val c1 = MyClass(1,"t")
        val c2 = MyClass(2,"t")
        assertFalse(c1.equals(c2))
    }

    test fun IfToStringReturnsTheCorrectString()
    {
        val c1 = MyClass(1, "t")
        assertEquals("MyClass(i=1, s=t)",c1.toString())
    }
}

But you can just as well do this.

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package demo.test
 
import demo.MyClass
import org.junit.Assert.assertEquals
import org.junit.Test as test
import org.junit.Assert.assertTrue
import org.junit.Assert.assertFalse
 
public class tests
{
    test fun IfPropertyIIsSetCorrectly()
    {
        val c1 = MyClass(1,"t")
        assertEquals(1,c1.i)
    }
 
    test fun IfPropertySIsSetCorrectly()
    {
        val c1 = MyClass(1,"t")
        assertEquals("t",c1.s)
    }
 
    test fun If2MyClassesWithTheSamePropertiesAreEqual()
    {
        val c1 = MyClass(1,"t")
        val c2 = MyClass(1,"t")
        assertTrue(c1.equals(c2))
    }
 
    test fun If2MyClassesWithDifferentPropertiesAreNotEqual()
    {
        val c1 = MyClass(1,"t")
        val c2 = MyClass(2,"t")
        assertFalse(c1.equals(c2))
    }
 
    test fun IfToStringReturnsTheCorrectString()
    {
        val c1 = MyClass(1, "t")
        assertEquals("MyClass(i=1, s=t)",c1.toString())
    }
}
package demo.test

import demo.MyClass
import org.junit.Assert.assertEquals
import org.junit.Test as test
import org.junit.Assert.assertTrue
import org.junit.Assert.assertFalse

public class tests
{
    test fun IfPropertyIIsSetCorrectly()
    {
        val c1 = MyClass(1,"t")
        assertEquals(1,c1.i)
    }

    test fun IfPropertySIsSetCorrectly()
    {
        val c1 = MyClass(1,"t")
        assertEquals("t",c1.s)
    }

    test fun If2MyClassesWithTheSamePropertiesAreEqual()
    {
        val c1 = MyClass(1,"t")
        val c2 = MyClass(1,"t")
        assertTrue(c1.equals(c2))
    }

    test fun If2MyClassesWithDifferentPropertiesAreNotEqual()
    {
        val c1 = MyClass(1,"t")
        val c2 = MyClass(2,"t")
        assertFalse(c1.equals(c2))
    }

    test fun IfToStringReturnsTheCorrectString()
    {
        val c1 = MyClass(1, "t")
        assertEquals("MyClass(i=1, s=t)",c1.toString())
    }
}

Notice the difference?

Conclusion

Not sure if kotlin.test adds much. We can just as well use Junit directly, you need it anyway.