unit-testing - jacoco 将行显示为未覆盖,但在运行代码时会执行这些行

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

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

enter image description here

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

@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 显示为未执行的行?请指教。

最佳答案

Jacoco 和 PowerMockito 不能一起工作。

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

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

这是一个 known issue .

关于unit-testing - jacoco 将行显示为未覆盖,但在运行代码时会执行这些行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36996471/

相关文章:

java - 乘以 AssertJ 断言中设置的条件?

java - Java ASM 方法的封面

c# - 使用NUnit测试属性为只读

android - 运行 gradlew 测试时出现 Robolectric 错误

java - 如何在不定义 equals() 和 hashCode() 的情况下比较自定义类列表?

junit - 使用 Maven 从 @Category 运行所有测试

java - 如何在要测试的类中模拟抽象类方法

java - 为随机数生成器编写 JUnit 测试

javascript - Jasmine + Istanbul 尔覆盖不起作用

perl - Devel::Cover 合并 Perl 脚本和模块的覆盖率数据