jmockit / jmockit1

Advanced Java library for integration testing, mocking, faking, and code coverage
Other
465 stars 240 forks source link

NullPointerException is thrown by mocked instance in Expectations in JMockit 1.12 #89

Closed Varun200864 closed 10 years ago

Varun200864 commented 10 years ago

Mocked instance throws NullPointerException in Expectations() with JMockit 1.12 but it runs successfully with JMockit 1.11.

rliesenfeld commented 10 years ago

I will need an example test; otherwise I've no idea how to produce the NPE.

Varun200864 commented 10 years ago

public class TestClass { @Mocked ClassToMock mockedInstance; @Test public void testMethodToTest() { ClassToTest classToTestInstance = new ClassToTest(); Object someObject = new Object(); new Expectations() { mockedInstance.methodToMock();//NullPointerException returns(someObject); } } }

Some test cases pass but some test cases throw exception.

rliesenfeld commented 10 years ago

Running the example test, there is no NullPointerException for me...

Varun200864 commented 10 years ago

Yes. But in my other code there is NullPointerException. That is proprietary code. So I can not post that code. Here is the stack trace java.lang.NullPointerException at mockit.internal.state.MockedTypeCascade.createNewCascadedInstanceOrUseNonCascadedOneIfAvailable(MockedTypeCascade.java:208) at mockit.internal.state.MockedTypeCascade.getCascadedInstance(MockedTypeCascade.java:161) at mockit.internal.state.MockedTypeCascade.getMock(MockedTypeCascade.java:66) at mockit.internal.expectations.invocation.ExpectedInvocation.produceCascadedInstanceIfApplicable(ExpectedInvocation.java:392) at mockit.internal.expectations.invocation.ExpectedInvocation.getDefaultValueForReturnType(ExpectedInvocation.java:380) at mockit.internal.expectations.RecordPhase.handleInvocation(RecordPhase.java:72) at mockit.internal.expectations.RecordAndReplayExecution.recordOrReplay(RecordAndReplayExecution.java:220) at ClassToMock at ClassToTest at TestClass 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:483) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at mockit.integration.junit4.internal.JUnit4TestRunnerDecorator.executeTestMethod(JUnit4TestRunnerDecorator.java:142) at mockit.integration.junit4.internal.JUnit4TestRunnerDecorator.invokeExplosively(JUnit4TestRunnerDecorator.java:71) at mockit.integration.junit4.internal.MockFrameworkMethod.invokeExplosively(MockFrameworkMethod.java:40) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Varun200864 commented 10 years ago

Thank you. This issue is resolved. But some other test cases started failing when I downloaded this https://github.com/jmockit/jmockit1/commit/577b334655de5b41ce5f11333827f31f91f18f96 code. Cases which has result = new MyException() in Expectations() are not working properly. Method which I mocked to throw MyException is not throwing Exception.