【问题标题】:jacoco shows lines as not covered though the lines get executed while running the codejacoco 显示未覆盖的行,尽管在运行代码时会执行这些行
【发布时间】:2016-08-28 01:06:50
【问题描述】:

我有以下代码行,Jacoco 将其指示为未“执行”。

但是当我调试测试用例时,它确实执行了这些行。下面是我写的测试用例。

@PrepareForTest({MessagingAdapterFactory.class, MessagingConfigReaderFactory.class,UpdaterServiceExecutor.class,Files.class})
    @Test
    public void should_shutDown_the_scheduledExecutor_and_close_the_messagingAdapter() throws Exception {
        PowerMockito.mockStatic(Files.class);
        PowerMockito.when(Files.exists(any())).thenReturn(true);

        PowerMockito.mockStatic(MessagingAdapterFactory.class);
        PowerMockito.when(MessagingAdapterFactory.getMessagingAdapter("edgeNode")).thenReturn(messagingAdapterMock);
        PowerMockito.mockStatic(MessagingConfigReaderFactory.class);
        PowerMockito.when(MessagingConfigReaderFactory.getConfigurationReader()).thenReturn(readerMock);

        ScheduledExecutorService scheduledExecutorServiceMock = Mockito.mock(ScheduledExecutorService.class);

        PowerMockito.mockStatic(Executors.class);
        PowerMockito.when(Executors.newSingleThreadScheduledExecutor()).thenReturn(scheduledExecutorServiceMock);

        when(readerMock.getConfigParams()).thenReturn("somePath,somePath,somePath");
        when(decompressUtilMock.decompressZip(Matchers.anyString(),Matchers.anyString())).thenReturn(true);
        when(checkSumUtilMock.check(anyString(), anyString())).thenReturn(true);
        when(commandExecutorMock.executeCommand("somePath verify /pa somePathKubeUpdates/KubePlatformSetup.exe")).thenReturn(false);
        updaterServiceExecutor.execute();
        Thread.sleep(10000);
        updaterServiceExecutor.close();

        verify(scheduledExecutorServiceMock,timeout(10000).times(1)).shutdownNow();
        verify(messagingAdapterMock,timeout(10000).times(1)).close();
    }

    @PrepareForTest({MessagingAdapterFactory.class, MessagingConfigReaderFactory.class,UpdaterServiceExecutor.class,Files.class})
    @Test
    public void should_not_throw_ServiceSDKException_when_occurred_while_closing_the_messagingAdapter() throws Exception {
        PowerMockito.mockStatic(Files.class);
        PowerMockito.when(Files.exists(any())).thenReturn(true);

        PowerMockito.mockStatic(MessagingAdapterFactory.class);
        PowerMockito.when(MessagingAdapterFactory.getMessagingAdapter("edgeNode")).thenReturn(messagingAdapterMock);
        PowerMockito.mockStatic(MessagingConfigReaderFactory.class);
        PowerMockito.when(MessagingConfigReaderFactory.getConfigurationReader()).thenReturn(readerMock);

        ScheduledExecutorService scheduledExecutorServiceMock = Mockito.mock(ScheduledExecutorService.class);

        PowerMockito.mockStatic(Executors.class);
        PowerMockito.when(Executors.newSingleThreadScheduledExecutor()).thenReturn(scheduledExecutorServiceMock);

        when(readerMock.getConfigParams()).thenReturn("somePath,somePath,somePath");
        when(decompressUtilMock.decompressZip(Matchers.anyString(),Matchers.anyString())).thenReturn(true);
        when(checkSumUtilMock.check(anyString(), anyString())).thenReturn(true);
        when(commandExecutorMock.executeCommand("somePath verify /pa somePathKubeUpdates/KubePlatformSetup.exe")).thenReturn(false);
        doThrow(new ServiceSDKException()).when(messagingAdapterMock).close();

        updaterServiceExecutor.execute();
        Thread.sleep(10000);
        updaterServiceExecutor.close();

        verify(scheduledExecutorServiceMock,timeout(10000).times(1)).shutdownNow();
        verify(messagingAdapterMock,timeout(10000).times(1)).close();
    }

这里有什么问题?为什么 Jacoco 显示为行尚未执行?请指教。

【问题讨论】:

    标签: unit-testing junit code-coverage jacoco jacoco-maven-plugin


    【解决方案1】:

    Jacoco 和 PowerMockito 不能一起工作。

    Jacoco 检测字节码,收集覆盖数据,然后根据类的某些标识符将覆盖信息与源代码相关联。

    PowerMockito 也检测字节码,这会导致不同的类标识符,因此 Jacoco 计算的覆盖率无法与源代码相关联,因为标识符信息不匹配。

    这是known issue

    【讨论】:

    • 但是我得到了其他课程的报道。没有解决办法吗?
    • 对于其他类,您可能不使用静态模拟。模拟实例(标准的 Mockito 方式)不受影响。
    【解决方案2】:

    杰拉德的回答就是原因。仅当您将正在测试的类放入 @PrepareForTest 时才会发生这种情况。所以我从某些方法中删除了它,现在它工作正常。拥有 PowerMockito 本身不会导致任何问题。仅当您在 @PrepareForTest 中有类名时才会出现问题。想办法只使用静态方法类的名称而不是您正在为其编写测试用例的类的名称来管理它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-24
      • 2016-01-20
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      相关资源
      最近更新 更多