模拟最后一堂课:powermock + Easymock +测试
我是 PowerMock 的新手,找不到符合我情况的示例。
这并不复杂——我正在尝试使用 PowerMock+EasyMock 来模拟 TestNG 测试中的最终类。
这是最终类:
public final class MyFinalClass {
private String myString = "someString";
public final String getMyString() {
return myString;
}
}
被测试的类委托给最终类的实例:
public class MyActiveClass {
private MyFinalClass finalClass;
public String getFinalString() {
return finalClass.getMyString();
}
public void setFinalClass(MyFinalClass finalClass) {
this.finalClass = finalClass;
}
}
这是测试类:
import static org.easymock.EasyMock.expect;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.Assert;
import org.testng.annotations.Test;
@PrepareForTest({ MyFinalClass.class })
public class MyTestClass extends PowerMockTestCase {
private MyActiveClass activeClass = new MyActiveClass();
private MyFinalClass mockFinalClass;
@Test
public void getFinalString() {
String expectedString = "mockedReturnString";
mockFinalClass = PowerMock.createMock(MyFinalClass.class);
activeClass.setFinalClass(mockFinalClass);
expect(mockFinalClass.getMyString()).andReturn(expectedString);
PowerMock.replayAll();
Assert.assertEquals(activeClass.getFinalString(), expectedString);
PowerMock.verifyAll();
}
}
但是当我运行测试时,我收到非法参数异常,抱怨 MyFinalClass 不是接口:
FAILED: getFinalString
java.lang.IllegalArgumentException: MyFinalClass is not an interface
at java.lang.reflect.Proxy.getProxyClass(Unknown Source)
at java.lang.reflect.Proxy.newProxyInstance(Unknown Source)
at org.easymock.internal.JavaProxyFactory.createProxy(JavaProxyFactory.java:24)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:51)
at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:2212)
at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2163)
at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:89)
at MyTestClass.getFinalString(MyTestClass.java:22)
...
我缺少什么?预先感谢您的帮助!
I'm new to PowerMock and can't find an example that matches my situation.
It's nothing complicated-- I am trying to use PowerMock+EasyMock to mock a final class in a TestNG test.
Here's the final class:
public final class MyFinalClass {
private String myString = "someString";
public final String getMyString() {
return myString;
}
}
The class under test delegates to an instance of the final class:
public class MyActiveClass {
private MyFinalClass finalClass;
public String getFinalString() {
return finalClass.getMyString();
}
public void setFinalClass(MyFinalClass finalClass) {
this.finalClass = finalClass;
}
}
And here's the Test class:
import static org.easymock.EasyMock.expect;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.Assert;
import org.testng.annotations.Test;
@PrepareForTest({ MyFinalClass.class })
public class MyTestClass extends PowerMockTestCase {
private MyActiveClass activeClass = new MyActiveClass();
private MyFinalClass mockFinalClass;
@Test
public void getFinalString() {
String expectedString = "mockedReturnString";
mockFinalClass = PowerMock.createMock(MyFinalClass.class);
activeClass.setFinalClass(mockFinalClass);
expect(mockFinalClass.getMyString()).andReturn(expectedString);
PowerMock.replayAll();
Assert.assertEquals(activeClass.getFinalString(), expectedString);
PowerMock.verifyAll();
}
}
But when I run the test, I get an illegal argument exception complaining that MyFinalClass is not an interface:
FAILED: getFinalString
java.lang.IllegalArgumentException: MyFinalClass is not an interface
at java.lang.reflect.Proxy.getProxyClass(Unknown Source)
at java.lang.reflect.Proxy.newProxyInstance(Unknown Source)
at org.easymock.internal.JavaProxyFactory.createProxy(JavaProxyFactory.java:24)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:51)
at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:2212)
at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2163)
at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:89)
at MyTestClass.getFinalString(MyTestClass.java:22)
...
What am I missing? Thanks in advance for your help!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是一些选项:
MyFinalClass
引入一个新接口并模拟该接口。MyFinalClass
的权限,则可以使用包装器对象,该对象委托给MyFinalClass
并在测试中模拟包装器对象。Here are some options:
MyFinalClass
and mock that interface instead.MyFinalClass
, you could use a wrapper object, which delegates toMyFinalClass
and mock the wrapper object in your test.