mockito中的模拟方法返回空指针异常?

时间:2018-05-06 04:34:23

标签: java spring-boot junit mockito

我正在编写一个从另一个服务调用方法的测试:

  // Setup mocks
  private GoalService goalService;
  private Principal principal;
  private Goal goal;
  private SecurityService securityService;

  @Before
  public void setup() {
    goalService = new GoalService();
    principal = mock(Principal.class);
    goal = mock(Goal.class);
    securityService = mock(SecurityService.class, Mockito.RETURNS_DEEP_STUBS);
  }

  @Test
  public void testRequesterOwnsGoalIsTrue() {
    doReturn(1).when(securityService).getUserIdByPrincipal(principal);
    when(goal.getUsersUserId()).thenReturn(1);
    boolean userOwnsGoal = goalService.requesterOwnsGoal(principal, goal);
    assertEquals(true, userOwnsGoal);
  }

在调试期间,当我呼叫securityService.getUserIdByPrincipal(principal)时,我得到了一个N​​PE。我不知道为什么。理想情况下,我想要的是方法调用返回1,我不关心在单元测试中测试那些函数内部。

实际代码

  Boolean requesterOwnsGoal(Principal principal, Goal goal) {
    Optional<Goal> optionalGoal = Optional.ofNullable(goal);
    if (optionalGoal.isPresent()) {
      return secService.getUserIdByPrincipal(principal) == optionalGoal.get().getUsersUserId();
    }
    throw new StandardUserException("Requesting user does not have access to goal");
  }

堆栈:

java.lang.NullPointerException
    at com.habicus.core.service.Goal.GoalService.requesterOwnsGoal(GoalService.java:170)
    at com.habicus.core.service.Goal.GoalServiceTest.testRequesterOwnsGoalIsTrue(GoalServiceTest.java:82)
    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.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

2 个答案:

答案 0 :(得分:1)

应该{​​{1}}才能使其正常工作。

doReturn(1).when(securityService).getUserIdByPrincipal(any(principal));

答案 1 :(得分:0)

到目前为止显示的代码表明您实际上正在创建一个模拟对象。但是你并没有注入那个模拟到你正在测试的类中!

仅仅创建模拟是不够的。您必须确保它们被您正在测试的代码使用!

这很可能是你的问题,但由于我们还没有[mcve],我们无法确定。