'Using PowerMockito with ArgumentCaptor
I simply try to test the following part of my service method:
if (isDeleted) {
LoggingUtils.info("Deleted. ingredientUuuid: {}", ingredient.getUuid());
}
I try to use the following approach in order to catch the values passed to the static method:
// @RunWith(MockitoJUnitRunner.class)
@RunWith(PowerMockRunner.class)
public class DemoTest {
@Test
public void test() {
// code omitted
Page<IngredientDTO> result = ingredientService.findAll(request,sort);
PowerMockito.mockStatic(LoggingUtils.class);
ArgumentCaptor<String> arg1 = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<UUID> arg2 = ArgumentCaptor.forClass(UUID.class);
PowerMockito.verifyStatic(LoggingUtils.class, times(1));
LoggingUtils.info(arg1.capture(), arg2.capture());
// ...
}
}
When debugging the test, it throws "The class com.company.common.LoggingUtils not prepared for test." pointing the following line:
PowerMockito.mockStatic(LoggingUtils.class);
So, how can I fix this problem?
Solution 1:[1]
Late answer, sorry for that, but maybe it still helps: Maybe you simply forgot the @PrepareForTest for your static class?
// @RunWith(MockitoJUnitRunner.class)
@RunWith(PowerMockRunner.class)
@PrepareForTest(LoggingUtils.class) // <-- Required, otherwise you'll get exactly that "not prepared for test" error
public class DemoTest {
[...]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nemax |