Mock
1,@PrepareForTest 出现时 @RunWith(PowerMockRunner.class) 必须出现.
2,代码中用到系统静态类 如:java.lang.System
public class SystemPropertyMockDemo {
public String getSystemProperty() throws IOException {
return System.getProperty("property");
@RunWith(PowerMockRunner.class)
@PrepareForTest({SystemPropertyMockDemo.class})//声明要Mock的类
public class SystemPropertyMockDemoTest {
public void demoOfFinalSystemClassMocking() throws Exception {
PowerMock.mockStatic(System.class);//Mock静态方法
EasyMock.expect(System.getProperty("property")).andReturn("my property");//录制Mock对象的静态方法
PowerMock.replayAll();//重放Mock对象
Assert.assertEquals("my property",
new SystemPropertyMockDemo().getSystemProperty());