Mockito stubbing thenAnswer抛出空指针异常

时间:2013-11-25 14:00:34

标签: java mockito junit4

我对Mockito相当新,我正在尝试编写一个测试用例,我需要控制返回值,所以我使用了Mockito提供的“thenAnswer”方法。但是我遇到了java.lang.NullPointerException,我无法找出原因。这是我的示例代码:

@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有一个名为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;
  }

我在这里做错了什么? 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)

1 个答案:

答案 0 :(得分:2)

public Contact answer(InvocationOnMock invocation) throws Throwable {
    Contact testContact = new Contact();
    contact.setId("12345");
    return testContact;
  }

contact更改为testContact