我使用EasyMock3.1/testNg-5.7/Powermock-easymock-1.5-full。代码很简单,只是模拟一个公共静态方法。
import static org.powermock.api.easymock.PowerMock.createMock;
import static org.powermock.api.easymock.PowerMock.mockStatic;
import static org.powermock.api.easymock.PowerMock.replay;
import static org.powermock.api.easymock.PowerMock.verify;
import static org.easymock.EasyMock.expect;
@PrepareForTest( {MyStatic.class})
public class StaticTest extends PowerMockTestCase{
@Test
public void testStaticMethod()
{
mockStatic( MyStatic.class );
expect( MyStatic.getString() ).andReturn( "good" );
replay( MyStatic.class );
MyStatic.getString();
verify(MyStatic.class);
}
}
我正在使用testng-eclipse插件,它在eclipse中工作得很好。但是当我尝试通过ant运行它时,我得到:
java.lang.IllegalStateException: no last call on a mock available
at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:520)
at org.easymock.EasyMock.expect(EasyMock.java:498)
它在代码"expect(MyStatic.getString())"处报错。andReturn("good");"
我的ant脚本是:
<testng classpathref="test.runtime.classpath" outputDir="${report.absolute.path}/testngreport" workingDir="${test.module.basedir}">
<!--<jvmarg line="-Xdebug -Xrunjdwp:transport=dt_socket,address=1044,server=y,suspend=y"/>-->
<classfileset dir="${test.module.testoutput.dir}" includes="**/*.class"/>
</testng>
我感谢任何反馈,谢谢!
比较一下,在ant中运行时(通过junit)尝试使用@RunWith(PowerMockRunner.class)注释运行。@RunWith注释看起来是静态方法测试示例与powermock文档中的示例之间的唯一区别。
这可能是一个测试错误。我试着复制你在蚂蚁上的失败,但一切都很顺利。以下是我使用的jar文件:
$ ls -1 lib/
cglib-nodep-2.2.3.jar
easymock-3.1.jar
javassist.jar
objenesis-1.4.jar
powermock-easymock-1.5.1-full.jar
testng-6.8.jar