@Test
public void testMockSayHello() {
PowerMockito.spy(DemoStatic.class);
PowerMockito.when(DemoStatic.sayHello()).thenReturn("my hello");
System.out.println(DemoStatic.sayHello()); // my hello
}
Mock 带参数的静态方法
@Test
public void testSaySomething() throws Exception {
PowerMockito.spy(DemoStatic.class);
PowerMockito.when(DemoStatic.class, "saySomething", Mockito.anyString()).thenReturn("something to say!");
System.out.println(DemoStatic.saySomething("say hello")); //something to say!
}
Mock private 静态方法
@Test
public void testMockPrivate() throws Exception {
PowerMockito.spy(DemoStatic.class);
PowerMockito.when(DemoStatic.class, "getMyWord").thenReturn("Nothing to say");
DemoStatic.sayAgain(); //Nothing to say
}