这是我的间谍声明
OAuthService oAuthServiceMock = Mockito.mock(OAuthService.class);
Mockito.doReturn(oAuthServiceMock).when(inviteServiceSpy.buildOAuthService(RESOURCE_URL, CONSUMER_KEY, CONSUMER_SECRET));
这是实际课程中的方法
public OAuthService buildOAuthService(String RESOURCE_URL, String CONSUMER_KEY, String CONSUMER_SECRET) {
return new ServiceBuilder()
.provider(new DummyOAuth1ApiProvider(RESOURCE_URL))
.apiKey(CONSUMER_KEY)
.apiSecret(CONSUMER_SECRET)
.signatureType(SignatureType.Header).debug().build();
}
这是我的测试用例的片段,以及实际的侦听类的实例化(包含上面的方法)
设定:
//This is in Setup/@Before method
myServiceSpy = Mockito.spy(new myServiceImpl());
测试用例:
@Test
public void testGetmyInviteData() throws Exception {
assertNotNull(myServiceSpy.getmyInviteData(signup_token,CONSUMER_KEY,CONSUMER_SECRET,RESOURCE_URL, RESOURCE_URL));
}
问题在于,代替间谍返回模拟对象,实际的方法似乎被称为引发空指针异常.这是我运行此测试时遇到的异常.关于我可能做错了什么以及如何使其变得更好并避免调用实际方法的任何想法?
java.lang.NullPointerException
at org.mycompany.myapp.service.MyServiceImplTest.testGetMyInviteData(MyServiceImplTest.java:143)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
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 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
解决方法:
您的模拟语句不正确. It should be:
doReturn(oAuthServiceMock).when(inviteServiceSpy).buildOAuthService(RESOURCE_URL, CONSUMER_KEY, CONSUMER_SECRET);
您的代码中发生的事情是buildOAuthService确实被调用,这导致您的异常.