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.
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.