发布网友 发布时间:2022-04-23 17:08
共1个回答
热心网友 时间:2023-10-10 06:05
下面我结合EasyMock给一个简单的例子:Java代码importjava.io.IOException;publicclassSystemPropertyMockDemo{publicStringgetSystemProperty()throwsIOException{returnSystem.getProperty("property");}}Java代码importorg.easymock.EasyMock;importorg.junit.Assert;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.powermock.api.easymock.PowerMock;importorg.powermock.core.classloader.annotations.PrepareForTest;importorg.powermock.moles.junit4.PowerMockRunner;@RunWith(PowerMockRunner.class)@PrepareForTest({SystemPropertyMockDemo.class})publicclassSystemPropertyMockDemoTest{@TestpublicvoiddemoOfFinalSystemClassMocking()throwsException{PowerMock.mockStatic(System.class);EasyMock.expect(System.getProperty("property")).andReturn("myproperty");PowerMock.replayAll();Assert.assertEquals("myproperty",newSystemPropertyMockDemo().getSystemProperty());PowerMock.verifyAll();}}测试用例里面增加了PrepareForTest注解,表明要测试的类,里面包含静态方法的调用。然后在测试类里面指定要测试的静态类和它的方法。实际上PowerMock是在EasyMock上面做了扩展,这些写法都是和EasyMock相同的。今天先写这么一个简单的例子,然后以后会将用到的都写上来。当然了,的可以参考:http://code.google.com/p/powermock/另外它的maven依赖如下,我在使用的时候就弄错了Xml代码org.powermockpowermock-mole-junit41.4.8testorg.powermockpowermock-api-easymock1.4.8test