简体   繁体   中英

jacoco shows lines as not covered though the lines get executed while running the code

I have the below lines of code of which are indicated as not "executed" by Jacoco.

在此处输入图片说明

But when I debug the test case it does execute those lines. Below are the test cases I wrote.

@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();
    }

What is wrong here? Why is Jacoco showing as the lines have not been executed? Please advice.

Jacoco and PowerMockito don't work together.

Jacoco instruments the byte code, collect the coverage data and afterwards associates the coverage information with the sourcecode based on some identifier of the class.

PowerMockito instruments the bytecode as well, this leads to different class identifiers so coverage calculated by Jacoco can not be associated to the source code because the identifier information does not match.

This is a known issue .

Gerald's answers is the reason. This only occurs when you have put the class being tested inside @PrepareForTest. So I removed that from certain methods and now its working fine. Having PowerMockito itself doesn't cause any issues. Issues arise only if you have the class name in @PrepareForTest. Find ways to manage it with only the name of the static method class and not the class for which you are writing the test cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM