mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.1k stars 200 forks source link

If defaultAnswer is set to throw exception, then equals does not cause exception to be thrown #406

Closed aSemy closed 3 years ago

aSemy commented 3 years ago

I wanted to make a mock that would never be called. I set defaultAnswer to throw an exception for everything. This works for most methods, but not for equals()

Maybe this is an underlying Mockito or Kotlin issue?

In this example, it is mockedUUID.timestamp() that throws an exception (which is good), but mockedUUID.equals("false") does not (which is unexpected).

  @Test
  fun testDefaultAnswer() {
    val mockedUUID : UUID = mock(
        defaultAnswer = ThrowsException(IllegalStateException("default answer"))
    )

    assertFalse(mockedUUID.equals("false"))
    mockedUUID.timestamp()
  }
java.lang.IllegalStateException: default answer

    at java.base/java.util.UUID.timestamp(UUID.java:376)
    at com.Test.testDefaultAnswer(Test.kt:332)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at 
bohsen commented 3 years ago

@aSemy This might be intended behavior. This issue should probably be reported to the main Mockito project, as the implementation is delegated to Mockito.

aSemy commented 3 years ago

Mostly likely - thanks for taking a look.

I've moved on to a different bit of work so I'll close this issue.