3
我是PowerMock的新手,無法找到符合我的情況的示例。模擬最終課程:powermock + easymock + testng
這沒什麼複雜的 - 我試圖用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)
...
我在想什麼?在此先感謝您的幫助!!!