测试挂起功能-未发现测试(有时) [英] Testing suspending function - no tests were found (sometimes)

查看:14
本文介绍了测试挂起功能-未发现测试(有时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试测试挂起的Spring Kotlin@控制器函数时,我无法理解一个奇怪的行为:

@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
    }

} 

测试:

@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) {
//
//      }
    }

}

当我运行测试时,测试没有运行-Maven报告没有找到测试。 当我取消对for循环的注释时,测试将运行并通过。当我用println("")替换for循环时,测试也将运行并通过。当我将其替换为assertThat(1).isEqualTo(1)时,测试将不会运行。 有人能解释一下吗?

JUnit

只要测试的返回类型不是void,推荐答案就不会发现它。 请参见JUnit test methods can't return a value。我认为这对我遇到的问题来说是相当不幸的。

这篇关于测试挂起功能-未发现测试(有时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆