I got this test class:
@ExtendWith(SpringExtension.class)
@RunWith(PowerMockRunner.class)
@PrepareForTest({StaticClass.class})
public class ClassNameTest {
@InjectMocks
private ClassNameTest classNameTest;
@Mock
private DependencyClass dependencyClass;
/* Other dependencies annotated with @Mock */
@Test
public void testFunction() {
/* Mocking of the dependencies using Mockito.when() and friends. */
PowerMockito.spy(StaticClass.class);
Powermockito.when(StaticClass.function(Mockito.any()).thenReturn(/* The object function returns */);
/* Rest of the test code. */
}
}
The function I'm testing has somewhere inside a line that goes like this:
ReturnObject ro = StaticClass.function(parameter);
And obviously, what I want is to prevent StaticClass to be called and return a specially crafted object. Yeah, I know this static class is not the best approach, but that's outside the scope of my current problem.
My problem is that, when debugging the test code, it actually enters into StaticClass.function when calling Powermockito.when
, and it fails there because somehow, enters with a null value.
All tutorials and guides I've seen says that this is basically what I need to do, so I'm completely lost here. Any ideas?