2021-04-27

Mokito comparisons fails

Test method

@Test
fun firebaseRecordException_whenCallReportApiErrorNonFatal_shouldCallOnlyOnce() {
    val exception = Exception("Exception Message: Backend API error")
    doNothing().
    `when`(firebaseCrashlytics).setCustomKey(eq("Exception Message: "), eq("Backend API error"))
    doNothing().
    `when`(firebaseCrashlytics).recordException(eq(exception))

    firebaseLogServiceImplementation.reportApiErrorNonFatal(
        "Exception Message: Backend API error",
        LogError.BackendAPIError,
        null,
        null
    )

    // Verify
    verify(firebaseCrashlytics, times(1)).recordException(eq(exception))
}

Using this test method I need to verify that the firebaseCrashlytics.recordException(exception) method called one time.

Method

override fun reportApiErrorNonFatal(exceptionCause: String, error: LogError, body: String ? , requestId : String ? ) {
    val exception = Exception(exceptionCause)

    firebaseCrashlytics.setCustomKey(EXCEPTION_MESSAGE, error.getErrorMessage())
    // The request id will be helpful to investigate the exact error
    requestId?.let {
        FirebaseCrashlytics.getInstance().setCustomKey(REQUEST_ID, it)
    }
    // The full body will be helpful to investigate the exact error
    body?.let {
        FirebaseCrashlytics.getInstance().setCustomKey(EXCEPTION_BODY, it)
    }

    firebaseCrashlytics.recordException(exception)
}

This is the method that I used to report non-fatal exceptions to firebase.

Error log for test case

Argument(s) are different!Wanted:
    firebaseCrashlytics.recordException(
        java.lang.Exception: Exception Message: Backend API error
    ); 
-> at com.google.firebase.crashlytics.FirebaseCrashlytics.recordException(FirebaseCrashlytics.java: 284)
Actual invocations have different arguments:
    firebaseCrashlytics.setCustomKey(
        "Exception Message: ",
        "Backend API error"
    ); 
-> at jp.co.rakuten.pointclub.android.services.log.FirebaseLogServiceImplementation.reportApiErrorNonFatal(FirebaseLogServiceImplementation.kt: 27)
firebaseCrashlytics.recordException(
    java.lang.Exception: Exception Message: Backend API error
); 
-> at jp.co.rakuten.pointclub.android.services.log.FirebaseLogServiceImplementation.reportApiErrorNonFatal(FirebaseLogServiceImplementation.kt: 33)

Comparison Failure: <Click to see difference >

Argument(s) are different!
Wanted:
    firebaseCrashlytics.recordException(
        java.lang.Exception: Exception Message: Backend API error
    ); 
-> at com.google.firebase.crashlytics.FirebaseCrashlytics.recordException(FirebaseCrashlytics.java: 284)
Actual invocations have different arguments:
    firebaseCrashlytics.setCustomKey(
        "Exception Message: ",
        "Backend API error"
    ); 
-> at jp.co.rakuten.pointclub.android.services.log.FirebaseLogServiceImplementation.reportApiErrorNonFatal(FirebaseLogServiceImplementation.kt: 27)
firebaseCrashlytics.recordException(
    java.lang.Exception: Exception Message: Backend API error
); 
-> at jp.co.rakuten.pointclub.android.services.log.FirebaseLogServiceImplementation.reportApiErrorNonFatal(FirebaseLogServiceImplementation.kt: 33)

at com.google.firebase.crashlytics.FirebaseCrashlytics.recordException(FirebaseCrashlytics.java: 284)

Here it shows there is a comparison fail. But I could not understand how firebaseCrashlytics.setCustomKey involves for comparison since I need to verify only firebaseCrashlytics.recordException.

If someone have any idea what I did wrong. That will be a great help for me. Thanks in advance.



from Recent Questions - Stack Overflow https://ift.tt/2Qw30IO
https://ift.tt/eA8V8J

No comments:

Post a Comment