Shouldn't this work?

Two functions in a junit test:

assertEquals(unit1.isAlarm(), unit2.isAalrm())

Both functions return a Boolean.

I had to do this instead:

val one = unit1.isAlarm()
val two = unit2.isAlarm()
assertEquals(one, two)

Yes, the first one should work. Doesn’t it work for you?

It should work without the typo.

OK I wrote that post in haste over the weekend. Here is the code. Actually I was compairing the isAlarm() result from one nullable type and one not nullable type. I’m confused because they both produce Boolean values (not nullable Booleans) so why isn’t the result of isAlarm() being evaluated? Or is it and I’m missing something?

data class Alarm(val date : Long, val type : Int);

class Unit
{
    val alarmList = mutableListOf<Alarm>();

    fun isAlarm() = {alarmList.size > 0}
}

Here’s the unit test:

import org.junit.Test

import org.junit.Assert.*
import org.junit.Before

class UnitTest
{
    var one : Unit? = null;

    @Before
    fun setupOne()
    {
        one = Unit();
    }

    @Test
    fun getAlarmList()
    {
    }

    @Test
    fun isAlarm()
    {
        val two = Unit();
        assertEquals(one?.isAlarm(), two.isAlarm())
    }

What am I missing?

isAlarm as written doesn’t return a boolean, it returns a function (of type () -> Boolean), because you wrapped it in {}

try with this:

fun isAlarm() = alarmList.isNotEmpty()

Thank you! I keep getting tripped up by that in Kotlin.

There is a feature in the editor that will get intellij/android studio to display the actual return type used for the function even if you don’t write it (unless obvious). That feature may help you.

1 Like