模拟静态方法powermock在expect()时给出异常。
@Test
public void testRegistrarService()
{
mockStatic(IdGenerator.class);
expect(IdGenerator.generateNewId()).andReturn(42L);
long actualId=serTestObj.registerService();
replay(IdGenerator.class);
verify(IdGenerator.class);
assertEquals(42L,actualId);
}
public class ServiceRegistrator
{
public long registerService()
{
long id = IdGenerator.generateNewId();
return id;
}
}
public class IdGenerator
{
public static long generateNewId()
{
return System.currentTimeMillis();
}
}
例外是:
java.lang.IllegalStateException: no last call on a mock available
at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:521)
at org.easymock.EasyMock.expect(EasyMock.java:499)
at home.powermock.testServiceRegistrator.testRegistrarService(testServiceRegistrator.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:31)
如何使用powerMock模拟staic方法 我正在使用intelliJ的想法,如何解决这个异常。
答案 0 :(得分:6)
您的代码缺少注释
@PrepareForTest(IdGenerator.class)
答案 1 :(得分:4)
在我的情况下,我在测试类中缺少以下方法
@ObjectFactory
/**
* Configure TestNG to use the PowerMock object factory.
*/
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
一旦我添加它,我就摆脱了“没有最后一次调用可用的模拟”错误。
答案 2 :(得分:3)
您需要在实际调用方法之前进行重播。
编辑:我认为部分问题可能是由于您的导入造成的。尽量不要导入静态powermock和静态easymock(我发现我常常迷惑自己,忘记了我需要调用哪一个重播)。
尝试运行以下代码。如果它没有正确运行,那么可能是因为你有特定版本的PowerMock / EasyMock / Junit的问题。
识别TestClass:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest(IdGenerator.class)
public class TestClass {
@Test
public void testRegistrarService()
{
ServiceRegistrator serTestObj = new ServiceRegistrator();
PowerMock.mockStatic(IdGenerator.class);
expect(IdGenerator.generateNewId()).andReturn(42L);
PowerMock.replay(IdGenerator.class);
long actualId=serTestObj.registerService();
PowerMock.verify(IdGenerator.class);
assertEquals(42L,actualId);
}
}
IdGenerator:
public class IdGenerator {
public static long generateNewId()
{
return System.currentTimeMillis();
}
}
ServiceRegistrator:
public class ServiceRegistrator {
public long registerService()
{
long id = IdGenerator.generateNewId();
return id;
}
}
答案 3 :(得分:2)
这个问题已经存在了很长时间,但我会尝试给它一个解释我为解决这个问题所做的工作。
首先,你必须使用这两个注释:
@RunWith(PowerMockRunner.class)
这个注释让当前的测试类知道用什么来运行他的测试,这很有用,因为我们可以使用PowerMockRunner而不是JUnitRunner
@PrepareForTest(IdGenerator.class)
此注释用于准备要在测试中使用的类“IdGenerator”,准备意味着我们将能够像公共方法一样模拟静态方法
添加这两个注释后,我们必须确保使用PowerMock提供的正确软件包:
1)PowerMock:
使用:我们将使用PowerMock使用以下代码行模拟(而不仅仅是)我们的静态方法
PowerMock.mockStatic(IdGenerator.class);
2)EasyMock:
使用:我们将使用EasyMock伪造我们的静态方法返回的对象:
EasyMock.expect(IdGenerator.generateNewId())andReturn(42L);
这是使用PowerMock和EasyMock的两个例子,在这里我将尝试解释代码及其作用:
mockStatic(IdGenerator.class);
//We mock our IdGenerator class which have the static object
expect(IdGenerator.generateNewId()).andReturn(42L);
//We fake our method return object, when we'll call generateNewId()
//method it will return 42L
//With expecting we "record" our this method and we prepare it to be
//changed (it will return our decided value)
replay(IdGenerator.class);
//We go to perform our methods "registered" with the expect method
//inside the IdGenerator class, in this case with replay we just apply
//the changes of the expect to the method generateNewId()
long actualId = serTestObj.registerService();
//We create our object (which inside have a non static method that
//use generateNewId() static method)
verify(IdGenerator.class);
//We verify that the our faked method have been called
assertEquals(42L,actualId);
//We see if the two values are matching
请注意,因为在创建将调用静态伪造方法的新对象(本示例中为actualId)之前必须使用重放。
对你正在使用的内容进行大量关注,因为我正在使用
PowerMockito.mockStatic(className.class);
//from import org.powermock.api.mockito.PowerMockito;
而不是
PowerMock.mockStatic(className.class);
//from import org.powermock.api.easymock.PowerMock;
我希望这个答案清晰完整
顺便说一下,我将向您介绍一些有用的链接:
PowerMock Static Documentation on GitHub
Mvn Repository PowerMock Libraries
见到你:D