Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argThat() causes TypeCastException #133

Closed
StepanOvsyankin opened this issue Dec 6, 2016 · 1 comment
Closed

argThat() causes TypeCastException #133

StepanOvsyankin opened this issue Dec 6, 2016 · 1 comment
Assignees
Labels
type:bug Behavior not according to spec
Milestone

Comments

@StepanOvsyankin
Copy link

StepanOvsyankin commented Dec 6, 2016

interface HelloSrv {
    fun sayHello(to: String): String
}

class AnyTest {
    @Test fun `should create non-null values`() {
        mock<HelloSrv> {
            on { sayHello(argThat { this == "A" }) } doReturn "A"
            on { sayHello(argThat { this == "B" }) } doReturn "B" // <--- fails here
        }
    }
}

fails with

kotlin.TypeCastException: null cannot be cast to non-null type kotlin.String

	at kkk.command.AnyTest$should create non-null values$mock$1$1$$special$$inlined$argThat$1.matches(Mockito.kt:55)
	at org.mockito.internal.invocation.TypeSafeMatching.apply(TypeSafeMatching.java:24)
	at org.mockito.internal.invocation.MatcherApplicationStrategy.forEachMatcherAndArgument(MatcherApplicationStrategy.java:82)
	at org.mockito.internal.invocation.InvocationMatcher.argumentsMatch(InvocationMatcher.java:152)
	at org.mockito.internal.invocation.InvocationMatcher.matches(InvocationMatcher.java:81)
	at org.mockito.internal.stubbing.InvocationContainerImpl.findAnswerFor(InvocationContainerImpl.java:82)
	at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:88)
	at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:32)
	at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:36)
	at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:41)
	at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptAbstract(MockMethodInterceptor.java:120)
	at kkk.command.HelloSrv$MockitoMock$934695312.sayHello(Unknown Source)
	at kkk.command.AnyTest$should create non-null values$mock$1$2.invoke(AnyTest.kt:16)
	at kkk.command.AnyTest$should create non-null values$mock$1$2.invoke(AnyTest.kt:12)
	at com.nhaarman.mockito_kotlin.KStubbing.on(Mockito.kt:119)
	at kkk.command.AnyTest.should create non-null values(AnyTest.kt:16)

mockito-kotlin: 1.0.0
mockito: 2.2.28

@nhaarman
Copy link
Collaborator

nhaarman commented Dec 7, 2016

This happens because the null value returned by the second argThat is propagated by Mockito into the first argThat, which does not accept null values.

For now, you can workaround this by using the Java version like so:

mock<HelloSrv> {
    on { sayHello(Mockito.argThat { it == "A" } ?: createInstance()) } doReturn "A"
    on { sayHello(Mockito.argThat { it == "B" } ?: createInstance()) } doReturn "B"
}

@nhaarman nhaarman added the type:bug Behavior not according to spec label Dec 7, 2016
@nhaarman nhaarman self-assigned this Dec 8, 2016
@nhaarman nhaarman added this to the 1.0.1 milestone Dec 10, 2016
@nhaarman nhaarman mentioned this issue Dec 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Behavior not according to spec
Projects
None yet
Development

No branches or pull requests

2 participants