[英]Mockito stubbing thenAnswer throws null pointer exception
I am fairly new to Mockito and I am trying to write a test case where I need control over the return value and so I am using "thenAnswer" method that Mockito provides. 我对Mockito还是相当陌生,我试图编写一个测试用例,需要控制返回值,因此我在使用Mockito提供的“ thenAnswer”方法。 However I am running into java.lang.NullPointerException and I am not able to figure out why. 但是,我遇到了java.lang.NullPointerException,但我无法弄清原因。 Here is my sample code: 这是我的示例代码:
@Before
public void setUp() throws Exception {
customerFactory = Mockito.mock(CustomerFactory.class);
contactChecker = new ContactChecker();
}
@After
public void tearDown() throws Exception {
customerFactory = null;
contactChecker = null;
}
@Test
public void testRetryGetContactFromCDS() {
Mockito.when(customerFactory.getContactByAccountId(Mockito.anyString())).thenAnswer(new Answer<Contact>() {
@Override
public Contact answer(InvocationOnMock invocation) throws Throwable {
Contact testContact = new Contact();
contact.setId("12345");
return testContact;
}
});
contactChecker.setCustomerFactory(customerFactory);
assertNotNull(contactChecker.retryGetContactFromCDS("stringThatDoesNotMatter"));
}
CutomerFactory has a method called getContactByAccountId and I use Customer Factory in ContactChecker class as follows: CutomerFactory有一个名为getContactByAccountId的方法,我在ContactChecker类中使用Customer Factory,如下所示:
public class ContactChecker {
private CustomerFactory customerFactory;
private static final int MAX_RETRY_TIME = 300000; // 5 mins. We give CDS 5 mins to create record we are looking for.
long startTimeForCDSContactChecking = System.currentTimeMillis();
long currentTime;
public CustomerFactory getCustomerFactory() {
return customerFactory;
}
public void setCustomerFactory(CustomerFactory customerFactory) {
this.customerFactory = customerFactory;
}
public String retryGetContactFromCDS(String webProfileId) {
currentTime = System.currentTimeMillis();
String contactId = null;
while((currentTime - startTimeForCDSContactChecking)/1000 < MAX_RETRY_TIME ) {
//Keep trying to get cds contact id
Contact contact = customerFactory.getContactByAccountId(webProfileId);
if (contact != null) {
contactId = contact.getId();
break;
}
currentTime = System.currentTimeMillis();
}
return contactId;
}
What might I be doing wrong here? 我在这里可能做错了什么? The complete stack trace for JUnit test is: JUnit测试的完整堆栈跟踪为:
java.lang.NullPointerException
at com.internal.licensing.consumer.ContactCheckerTest$1.answer(ContactCheckerTest.java:38)
at com.internal.licensing.consumer.ContactCheckerTest$1.answer(ContactCheckerTest.java:1)
at org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(StubbedInvocationMatcher.java:31)
at org.mockito.internal.MockHandler.handle(MockHandler.java:92)
at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:47)
at com.internal.business.license.customer.CustomerFactory$$EnhancerByMockitoWithCGLIB$$b9ace21e.getContactByMathWorksAccountId(<generated>)
at com.internal.licensing.consumer.ContactChecker.retryGetContactFromCDS(ContactChecker.java:22)
at com.internal.licensing.consumer.ContactCheckerTest.testRetryGetContactFromCDS(ContactCheckerTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
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)
public Contact answer(InvocationOnMock invocation) throws Throwable {
Contact testContact = new Contact();
contact.setId("12345");
return testContact;
}
change contact
to testContact
将contact
更改为testContact
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.