kotlintestingkotlin-coroutinesrsocket

Testing suspending function - no tests were found (sometimes)


I'm having trouble understanding a strange behavior when trying to test my suspending Spring Kotlin @Controller function:

@Controller
class PersonRSocketController(val personRepository: PersonRepository) {
    private val sharedFlow = flow<Person> {
        for (i in 1..1000) {
               emit(Person(i.toLong(), firstName = "$i - firstName", lastName = "$i - lastName"))
        }
    }.buffer(100).shareIn(GlobalScope, SharingStarted.WhileSubscribed(replayExpirationMillis = 0),replay = 0)

    @MessageMapping("currentpersons")
    suspend fun currentPersons(): Flow<Person?> {
        return sharedFlow
    }

} 

Test:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class RsocketServerAppTests @Autowired constructor(
    val rSocketRequesterBuilder: RSocketRequester.Builder,
    @LocalServerPort val serverPort: Int
) {
    @Test
    fun testRSocket() = runBlocking{
        val requestSpec =
            rSocketRequesterBuilder.websocket(URI.create("ws://localhost:$serverPort/rsocket")).route("currentpersons")
        var result = requestSpec.retrieveFlow<PersonDataRequest>()
        assertThat(result.first().firstName).isEqualTo("1 - firstName")
//      for (i in 1..2) {
//
//      }
    }

}

When I run the test the test is not run - Maven reports that no tests were found. When I uncomment the for-loop the test will run and pass. when I replace the for-loop with a println("") the test will also run and pass. When I replace it with assertThat(1).isEqualTo(1) the test will not run. Can somebody explain this?


Solution

  • The test is not discovered by JUnit whenever its return type is not void. See JUnit test methods can't return a value. I think this is quite unfortunate for the issue I came across.