Reputation: 1411
I am writing my Junit test cases for Groovy using Mockito jar, but it is giving me following exception
java.lang.NullPointerException
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at 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.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
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.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
So I have added mockito-groovy-support 1.3 version jar. but I am still facing the same issue. below is my code
package test.service
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import static org.mockito.Mockito.when;
//@RunWith(MockitoJUnitRunner.class)
class SyncImplTest {
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@InjectMocks
SyncThreatImpl fixture;
@Mock
RpcConfigurationLoader rpcConfigurationLoader
@Test
public void testRpcConfig(){
RpcApiInfo rpcApiInfo = new RpcApiInfo();
when(rpcConfigurationLoader.loadConfiguration()).thenReturn(rpcApiInfo) //here m getting NPE
}
}
this is my ss of the jars am using
Upvotes: 1
Views: 1390
Reputation: 38724
Again you are using libraries that are aaaaages old. Do NOT mix different Mockito versions. You now have Mockito 1.10.19 and 2.8.9 both in your classpath. mockito-groovy-support
is from 2013 and is written for Mockito 1.x, so most probably will not work with Mockito 2.x and you should really not mix two Mockito versions in your classpath.
I guess Mockito like most mocking frameworks just don't properly support mocking Groovy. The most mocking frameworks (JMockit too e. g.) are written for Java and wont work properly with Groovy as Groovy is too much a dynamic language.
You should maybe instead use the Groovy-built-in mocking as described at https://kahdev.wordpress.com/2013/02/14/mocking-java-classes-in-groovy-vs-mockito/
Or you could consider using Spock framework instead for writing tests and mocking. It is written in Groovy and works perfectly fine for Groovy, also with mocking and stubbing. It's syntax and how you write tests might look a bit alien at first, but if you get used to it, it is really an amazing test framework and it is built upon JUnit, so can be run and evaluated by any tool that supports JUnit tests.
Upvotes: 2