【问题标题】:Testing suspending function - no tests were found (sometimes)测试挂起功能 - 未找到测试(有时)
【发布时间】:2021-06-26 10:03:06
【问题描述】:

在尝试测试我的暂停 Spring Kotlin @Controller 函数时,我无法理解一个奇怪的行为:

@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) 替换它时,测试将不会运行。 有人可以解释一下吗?

【问题讨论】:

  • 您是否尝试过使用runBlockingTest()Here's 包裹。
  • 然后我得到了臭名昭著的“IllegalStateException: This job has not done yet”
  • 包含一个规则来同步转换所有函数怎么样。 android架构组件是@get:Rule val instantTaskExecutorRule = InstantTaskExecutorRule()

标签: kotlin testing kotlin-coroutines rsocket


【解决方案1】:

只要返回类型不是void,JUnit 就不会发现该测试。 见JUnit test methods can't return a value。我认为这对于我遇到的问题来说是非常不幸的。

【讨论】:

    猜你喜欢
    • 2018-04-16
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多