admin管理员组文章数量:1391937
I need to verify that exception been thrown or that some logging method been called. Here is my code
fun stop() {
println("----")
CoroutineScope(Dispatchers.IO).launch {
try {
internalMqttClient.disconnectForcibly()
} catch (e: Exception) {
internalLogger.logError(DEFAULT_LOG_TAG, "1111")
Here is my test
@Test
fun test2()= runTest{
every { internalMqttClient.disconnectForcibly() } throws Exception()
subject.stop()
advanceUntilIdle()
verify(exactly = 1) {
internalLogger.logError(
DEFAULT_LOG_TAG,
"1111") }
}
}
And here is the error
java.lang.AssertionError: Verification failed: call 1 of 1: InternalLogger(#3).logError(eq(mqttLibrary), eq(1111))) was not called
I need to verify that exception been thrown or that some logging method been called. Here is my code
fun stop() {
println("----")
CoroutineScope(Dispatchers.IO).launch {
try {
internalMqttClient.disconnectForcibly()
} catch (e: Exception) {
internalLogger.logError(DEFAULT_LOG_TAG, "1111")
Here is my test
@Test
fun test2()= runTest{
every { internalMqttClient.disconnectForcibly() } throws Exception()
subject.stop()
advanceUntilIdle()
verify(exactly = 1) {
internalLogger.logError(
DEFAULT_LOG_TAG,
"1111") }
}
}
And here is the error
java.lang.AssertionError: Verification failed: call 1 of 1: InternalLogger(#3).logError(eq(mqttLibrary), eq(1111))) was not called
Share
Improve this question
asked Mar 12 at 16:04
Bob RedityBob Redity
6311 gold badge8 silver badges25 bronze badges
1 Answer
Reset to default 0Try to parameterize the Dispatcher to be configurable according to the context. If the code uses Dispatchers.Main or Dispatchers.IO, it would be advisable to allow injecting it as a parameter. In this way, a TestDispatcher can be provided in the tests, ensuring that the execution occurs at the right time.
本文标签: androidHow to test that exception been thrown in MockkStack Overflow
版权声明:本文标题:android - How to test that exception been thrown in Mockk - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744741884a2622656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论