android - How to test that exception been thrown in Mockk - Stack Overflow

I need to verify that exception been thrown or that some logging method been called.Here is my codefu

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
Add a comment  | 

1 Answer 1

Reset to default 0

Try 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744741884a4591075.html

相关推荐

  • android - How to test that exception been thrown in Mockk - Stack Overflow

    I need to verify that exception been thrown or that some logging method been called.Here is my codefu

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信