We are migrating to JUnit5 and removing PowerMockito since Mockito now supports mockStatic. Before, PowerMockito offered Whitebox.setInternalState(..) making it possible to set values final fields (constants)
I have a class like
public class MyClass {
private static final String myField = Props.getString("myField");
public boolean myMethod() {
//uses my field
}
}
and my Test looks like this
class MyClassTest {
@Test
void myTest() {
try (MockedStatic<Props> propsMockedStatic = Mockito.mock(Props.class)) {
propsMockedStatic.when(() -> Props.getString("myField")).thenReturn("Hello!!");
MyClass myClass = new MyClass();
//more code
}
}
}
The problem is that when I introduce more tests I see that the myField
gets mocked only once when I run the Tests although I use a new instance every time. This results in braking tests since on other cases I want myField
to have different values but when I run them separately everything completes successfully