jmockit class 转换异常

jmockit class cast exception

我正在尝试模拟一些 webogic classes 和接口来测试执行此操作的方法:

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 class 实现了 IAssertion,所以我一直在尝试模拟 SimpleAssertion class,但仍然遇到相同的错误。

有人可以解释一下如何在这种情况下使用 jmockit 吗?

在这种情况下,您需要模拟 SimpleAssertion 实现 class,因为这是被测方法所期望的。在界面上使用 MockUp<IAssertion> 只会让你得到一个动态代理 class 而不是 SimpleAssertion,因此 ClassCastException.