【问题标题】:Mockito stubbing thenAnswer throws null pointer exceptionMockito 存根 thenAnswer 抛出空指针异常
【发布时间】:2013-11-25 14:00:33
【问题描述】:

我对 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"));
  }

CustomerFactory 有一个名为 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)

【问题讨论】:

  • ContactCheckerTest.java:38 的第 38 行是什么?

标签: java mockito junit4


【解决方案1】:
public Contact answer(InvocationOnMock invocation) throws Throwable {
    Contact testContact = new Contact();
    contact.setId("12345");
    return testContact;
  }

contact 更改为testContact

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-13
    • 2019-07-28
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    相关资源
    最近更新 更多