import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.core.classloader.annotations.PrepareForTest; @RunWith(PowerMockRunner.class) @PrepareForTest({StaticClass.class, MyClass.class}) public class MyTestClass { @Test public void testStaticMethod() { PowerMockito.mockStatic(StaticClass.class); PowerMockito.when(StaticClass.staticMethod()).thenReturn("mockedValue"); assertEquals("mockedValue", MyClass.methodUsingStaticMethod()); } @Test public void testPrivateMethod() throws Exception { MyClass myObject = PowerMockito.spy(new MyClass()); PowerMockito.doReturn("mockedValue").when(myObject, "privateMethod"); assertEquals("mockedValue", myObject.methodUsingPrivateMethod()); } @Test public void testConstructor() throws Exception { MyClass mockObject = PowerMockito.mock(MyClass.class); PowerMockito.whenNew(MyClass.class).withArguments("test").thenReturn(mockObject); assertEquals(mockObject, new MyClass("test")); } @Test public void testStaticInitializationBlock() { PowerMockito.mockStatic(TestClass.class); MyClass.methodWithStaticInitializationBlock(); PowerMockito.verifyStatic(TestClass.class); TestClass.staticInitializationMethod(); } }


上一篇:
下一篇:
切换中文