我正在尝试模拟一些webogic类和接口来测试执行此操作的方法:
void init(oracle.wsm.policy.model.IAssertion assertion,....) {
...
oracle.wsm.policy.model.IAssertionBindings bindings = ((oracle.wsm.policy.model.impl.SimpleAssertion) assertion).getBindings();
...
}
我的测试代码如下:
@Test
public final void testInit() {
IAssertion assertionMock = new MockUp<IAssertion>() {
@Mock
QName getQName() {
return new QName("qname");
}
}.getMockInstance();
CustomAuditPolicy custom = new CustomAuditPolicy();
custom.init(assertionMock);
我得到了:
java.lang.ClassCastException: $Impl_IAssertion cannot be cast to oracle.wsm.policy.model.impl.SimpleAssertion
at xxx.CustomAuditPolicy.init(CustomAuditPolicy.java:82)
at xxx.CustomAuditPolicyTest.testGetAssertionName(CustomAuditPolicyTest.java:190)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
SimpleAssertion类实现了IAssertion,所以我一直在尝试模拟SimpleAssertion类,但仍然遇到同样的错误。
有人可以解释一下如何在这种情况下使用jmockit吗?
答案 0 :(得分:1)
在这种情况下,您需要模拟SimpleAssertion
实现类,因为这是测试中的方法所期望的。在界面上使用MockUp<IAssertion>
只会获得一个不是SimpleAssertion
的动态代理类,因此ClassCastException
。