question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

shouldBe does not handle java long or Long but does handle Integer

See original GitHub issue

Getting a unexpected assertion failure while converting assertj assertions to kotlin-test shouldBe style assertions.

Given this simple Java class:

public class Foo {
    public static final long MEANING_OF_LIFE = 42l;
}

This test fails with the above on the shouldBe line:

import io.kotlintest.shouldBe
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Test

class FooTest {
    @Test
    fun fooTest() {
        val v1: Int = Foo.MEANING_OF_LIFE.toInt()
        val v2 = java.lang.Integer.valueOf(42)
        val v3 = Foo.MEANING_OF_LIFE
        val v4 = java.lang.Long.valueOf(42)
        val v5 = Foo.MEANING_OF_LIFE.toLong()

        // assertj works for all cases
        Assertions.assertThat(v1).isEqualTo(42)
        Assertions.assertThat(v2).isEqualTo(42)
        Assertions.assertThat(v3).isEqualTo(42)
        Assertions.assertThat(v4).isEqualTo(42)
        Assertions.assertThat(v5).isEqualTo(42)

        // works
        v1 shouldBe 42
        // works
        v2 shouldBe 42
        // fails
        v3 shouldBe 42
        // fails
        v4 shouldBe 42
        // fails
        v5 shouldBe 42
    }
}

with:

java.lang.AssertionError: expected: 42 but was: 42
Expected :42 
Actual   :42

So, it seems the only way I can make this work is calling toInt(). This seems to result from differences in the way kotlin numeric types differ from Java as explained here: https://kotlinlang.org/docs/reference/basic-types.html. But it makes testing Java code with Kotlin a bit more challenging. So, it might be worth investigating some kind of workaround and make shouldBe a bit more foregiving by maybe handling java primitive and boxed type explicitly.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sksamuelcommented, May 30, 2018

I’ve added a quick fix and some tests. Basically I promote ints to longs, and floats to doubles where appropriate.

See https://github.com/kotlintest/kotlintest/commit/48182db19c1d27b18c112165d7247a95addd3c0a

0reactions
sksamuelcommented, Jun 1, 2018

Fix is applied in latest release 3.1.5

Read more comments on GitHub >

github_iconTop Results From Across the Web

When to use Long vs long in java? - Stack Overflow
long is a primitive type, while Long is a Java class (and so it will inherit Object). long must be assigned with a...
Read more >
BigDecimal (Java Platform SE 8 ) - Oracle Help Center
Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale.
Read more >
How to Handle the NumberFormat Exception in Java - Rollbar
The NumberFormatException in Java is thrown when it is not possible to convert a string to a numeric type (e.g. int, float).
Read more >
BigInteger Class in Java - GeeksforGeeks
BigInteger class is used for the mathematical operation which involves very big integer calculations that are outside the limit of all ...
Read more >
Java Basics - Java Programming Tutorial
There are four integer types: 8-bit byte , 16-bit short , 32-bit int and 64-bit long . They are signed integers in 2's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found