shouldBe does not handle java long or Long but does handle Integer
See original GitHub issueGetting 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:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
Fix is applied in latest release 3.1.5