【问题标题】:Mockito test with ArgumentCaptor failing from time to time使用 ArgumentCaptor 的 Mockito 测试有时会失败
【发布时间】:2023-02-06 00:54:34
【问题描述】:

我正在使用 Java 17、Spring Boot 2.7.5(spring-boot-starter-test 正在将 JUnit Jupiter 和 Mockito 引入我的项目)。完整的项目代码 (WIP) 在这里:https://github.com/Tonypsilon/bmm.backend

我有一个要测试的方法:

    public void verifyUserIsClubAdminOfAnyClub(@NonNull String username, @NonNull Set<Long> clubIds) {
        if (clubIds.stream()
                .map(clubAdminService::getAdminsOfClub)
                .flatMap(Set::stream)
                .noneMatch(username::equals)) {
            throw new AccessDeniedException("...");
        }
    }

getAdminsOfClub 方法返回一组字符串。这是我对成功案例的测试:

    @Test
    void testVerifyUserIsClubAdminOfAnyClubSuccess() {
        String username = "username";
        Set<Long> clubIds = Set.of(1L, 2L);
        when(clubAdminService.getAdminsOfClub(1L)).thenReturn(Set.of("some user", "another user"));
        when(clubAdminService.getAdminsOfClub(2L)).thenReturn(Set.of("username", "some user"));

        ArgumentCaptor<Long> clubIdArgumentCaptor = ArgumentCaptor.forClass(Long.class);
        authorizationService.verifyUserIsClubAdminOfAnyClub(username, clubIds);
        verify(clubAdminService, times(2)).getAdminsOfClub(clubIdArgumentCaptor.capture());
        assertThat(clubIdArgumentCaptor.getAllValues()).containsExactlyInAnyOrder(1L, 2L);
    }

在我使用 Eclipse 运行该测试的情况下,该测试的通过率约为 80%。至此,在Intellij IDEA中运行通过。请注意,Eclipse 以某种方式需要大约一半的时间来执行测试。 当它失败时,这是堆栈跟踪:

org.mockito.exceptions.verification.TooFewActualInvocations: 
clubAdminService.getAdminsOfClub(
    <Capturing argument>
);
Wanted 2 times:
-> at de.tonypsilon.bmm.backend.security.rnr.service.AuthorizationServiceTest.testVerifyUserIsClubAdminOfAnyClubSuccess(AuthorizationServiceTest.java:39)
But was 1 time:
-> at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)

    at de.tonypsilon.bmm.backend.security.rnr.service.AuthorizationServiceTest.testVerifyUserIsClubAdminOfAnyClubSuccess(AuthorizationServiceTest.java:39)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
    at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:95)
    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:91)
    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:60)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

我认为这可能与通常执行得非常快(~0,01s)的测试用例有关,所以可能在那里并行发生了一些事情,其中​​一些事情在它应该完成之前完成了。但我不确定内部结构,我不希望出现这种行为。

所以我的下一个想法是我以错误的方式使用 ArgumentCaptor,因为我对它不是很有经验。我试图阅读有关它的信息,但找不到对我的具体案例有帮助的信息。有人能帮我吗?提前致谢!

【问题讨论】:

    标签: mockito argumentcaptor


    【解决方案1】:

    这可能不是您问题的确切答案,但我建议将此测试分成多个较小的测试:

    • 测试 getAdminsOfClub 的返回是否适用于没有出现管理员的情况
    • 测试 getAdminsOfClub 的返回是否适用于管理员的一次出现
    • 测试getAdminsOfClub 的返回是否适用于管理员的多次出现
    • 测试 getAdminsOfClub 方法为每个俱乐部调用一次

    这样你就不必使用ArgumentCaptor了:

    @Test
    void verifyUserIsClubAdminOfAnyClub_no_admin() {
      when(clubAdminService.getAdminsOfClub(anyLong()))
          .thenReturn(Set.of("some user", "another user"));
      assertThrows(
          AccessDeniedException.class,
          ()->authorizationService.verifyUserIsClubAdminOfAnyClub("userName", Set.of(1L)),
          "..."
      );
    }
    
    @Test
    void verifyUserIsClubAdminOfAnyClub_1_admin() {
      when(clubAdminService.getAdminsOfClub(anyLong()))
        .thenReturn(Set.of("userName", "another user"), Set.of("some user", "another user"));
      assertDoesNotThrow(
          ()->authorizationService.verifyUserIsClubAdminOfAnyClub("userName", Set.of(1L, 47L))
      );
    }
    
    @Test
    void verifyUserIsClubAdminOfAnyClub_2_admins() {
      when(clubAdminService.getAdminsOfClub(anyLong()))
          .thenReturn(Set.of("userName", "another user"), Set.of("some user", "userName"));
    
      assertDoesNotThrow(
          ()->authorizationService.verifyUserIsClubAdminOfAnyClub("userName", Set.of(1L, 47L))
      );
    }
    
    @Test
    void verifyUserIsClubAdminOfAnyClub_calls_getAdminsOfClub_for_each_club() {
      assertThrows(
          AccessDeniedException.class,
          ()->authorizationService.verifyUserIsClubAdminOfAnyClub("userName", Set.of(1L, 42L)),
          "..."
      );
      verify(clubAdminService).getAdminsOfClub(1L);
      verify(clubAdminService).getAdminsOfClub(42L);
    }
    

    assertThrows的第三个参数是预期的消息。我希望你没有"..." 字面上的消息。

    如果异常消息是动态的,我就不会在测试中指定第三个参数——只测试它是否对程序逻辑至关重要(不应该是这种情况)。

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 2022-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多