Use mockit.MockUp.MockUp() to mock public static methods. One caveat is that the mocked behaviour applies to other test methods as well. (test2() prints "abc", which is surprising, isn't it?)
@ExtendWith(value = {MockitoExtension.class})
@RunWith(JUnitPlatform.class)
class MyTest {
@Test
void test1() throws IOException {
new MockUp<System>() {
@mockit.Mock
public String getProperty(String key){
return "abc";
}
};
assertThat(System.getProperty("a"), is("abc"));
}
@Test
void test2() throws IOException {
System.out.println(System.getProperty("a")); //Prints abc
}
}
No comments:
Post a Comment