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

NullPointerException is thrown when stubbing a generic function using the lamda approach #104

Closed
Nejat opened this issue Oct 28, 2016 · 2 comments
Assignees

Comments

@Nejat
Copy link

Nejat commented Oct 28, 2016

here are examples to illustrate the issue ...

package bug.stubbing.lambda

import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.whenever
import com.winterbe.expekt.should
import org.junit.jupiter.api.Test

class LamdaStubbingIntialization { 

    // generic interface to illustrate stubbing issue
    interface GenericInterface<out T> {

        fun NonGeneric(): Double

        fun ASecondNonGeneric(): String

        fun Generic(): T
    }

    // stubbing a generic function through the lamda method fails
    // with a NullPointerException
    @Test
    fun FailedStubbingGenerics () {

        val mocked: GenericInterface<Int>

        try {
            mocked = mock {
                on { NonGeneric() }.thenReturn(-1.0)

                on { ASecondNonGeneric() }.thenReturn("multiple stubbing is not a problem")

                on { Generic() }.thenReturn(1)
            }
        } catch (ex: NullPointerException) {
            return@FailedStubbingGenerics
        }

        mocked.NonGeneric().should.be.equal(-1.0)

        mocked.ASecondNonGeneric().should.be.equal("multiple stubbing is not a problem")

        mocked.Generic().should.be.equal(1)

        // just signaling that it worked
        "unexpected pass".should.be.equal("means the mock worked")
    }

    // subbing a non-generic method of a generic interface is fine
    @Test
    fun PassingIncompleteStubbingGenerics () {

        val mocked: GenericInterface<Int> = mock {
            on { NonGeneric() }.thenReturn(-1.0)
        }

        mocked.NonGeneric().should.be.equal(-1.0)

        mocked.Generic().should.be.`null`
    }

    // stubbing a generic function through the non-lamda method works
    // so the problem is not stubbing a generic method
    @Test
    fun PassingStubbingGenerics () {

        val mocked: GenericInterface<Int> = mock()

        whenever(mocked.NonGeneric()).thenReturn(-1.0)
        whenever(mocked.Generic()).thenReturn(1)

        mocked.NonGeneric().should.be.equal(-1.0)

        mocked.Generic().should.be.equal(1)
    }
}
@nhaarman
Copy link
Collaborator

What is the stack trace of the exception?

@Nejat
Copy link
Author

Nejat commented Oct 29, 2016

after removing the try/catch from the FailedStubbingGenerics example, the following stack trace is produced

java.lang.NullPointerException
    at bug.stubbing.lambda.LamdaStubbingIntialization$FailedStubbingGenerics$1$3.invoke(LamdaStubbingIntialization.kt:45)
    at bug.stubbing.lambda.LamdaStubbingIntialization$FailedStubbingGenerics$1$3.invoke(LamdaStubbingIntialization.kt:8)
    at com.nhaarman.mockito_kotlin.KStubbing.on(Mockito.kt:96)
    at bug.stubbing.lambda.LamdaStubbingIntialization.FailedStubbingGenerics(LamdaStubbingIntialization.kt:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:252)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:114)
    at org.junit.jupiter.engine.descriptor.MethodTestDescriptor.lambda$invokeTestMethod$7(MethodTestDescriptor.java:210)
    at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
    at org.junit.jupiter.engine.descriptor.MethodTestDescriptor.invokeTestMethod(MethodTestDescriptor.java:206)
    at org.junit.jupiter.engine.descriptor.MethodTestDescriptor.execute(MethodTestDescriptor.java:155)
    at org.junit.jupiter.engine.descriptor.MethodTestDescriptor.execute(MethodTestDescriptor.java:63)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$0(HierarchicalTestExecutor.java:81)
    at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$0(HierarchicalTestExecutor.java:88)
    at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$0(HierarchicalTestExecutor.java:88)
    at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:51)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:124)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:84)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants