Junit 5 Assertions.fail() can not infer type in Kotlin

Viewed 2801

When I attempt to use JUnit 5 Assertions.fail in my Kotlin tests I get a compilation failure because parameter V can not be inferred:

import org.junit.jupiter.api.Assertions.fail
internal class MyTests {
   @Test
   fun simpleTest() {
     fail("Does not compile") 
   }
}

Of course a simple solution to this problem is:

import org.junit.jupiter.api.Assertions.fail
internal class MyTests {
   @Test
   fun simpleTest() {
     val result: Any = fail("Compiles") 
   }
}

However I do not wish to have to create an unused value in my code. Is there a way to define the type without having to declare a val? Also why does this happen in Kotlin? Java has no such problem with generics:

import org.junit.jupiter.api.Assertions.fail;
class MyJavaTests {
    @Test
    public void simpleTest() {
        fail("Compiles);
    }
}

EDIT: I discovered immediately after posting the question that the solution is to parameterize the call:

import org.junit.jupiter.api.Assertions.fail
internal class MyTests {
   @Test
   fun simpleTest() {
     fail<Any>("Does not compile") 
   }
 }

However still willing to accept an answer that can explain why I need to do this in kotlin.

1 Answers

Please see this issue: https://github.com/junit-team/junit5/issues/1209

It seems that this is already fixed in the junit-jupiter-api Assertions.kt file as a top-level function in org.junit.jupiter.api package.

Import the org.junit.jupiter.api.fail and not the org.junit.jupiter.api.Assertions.fail

Related