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

  // 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)时,我得到了NPE。我不知道为什么。理想情况下,我想要的是返回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)

最佳答案

要使其正常工作,应为doReturn(1).when(securityService).getUserIdByPrincipal(any(principal));

要么

doReturn(1).when(securityService).getUserIdByPrincipal(any(Principal.class));

09-29 21:30