【问题标题】:JAVA: java.lang.IllegalArgumentException: Cannot subclass final class class [Lcom.package.testEntityDO;JAVA:java.lang.IllegalArgumentException:不能继承最终类类[Lcom.package.testEntityDO;
【发布时间】:2015-10-25 15:01:31
【问题描述】:

我试图在课堂上模仿。

 public class testEntityDO extends BasetestDO {
    private String entityType;  
    private testCapabilityDO[] capabilities;
    private testEntityDO[] testDOs;
    public String getEntityType() {
        return entityType;
    }
    public void setEntityType(String entityType) {
        this.entityType = entityType;
    }
    public testCapabilityDO[] getCapabilities() {
        return capabilities;
    }
    public void setCapabilities(testCapabilityDO[] capabilities) {
        this.capabilities = capabilities;
    }
    public TestEntityDO[] getTestPortDOs() {
        return testPortDOs;
    }
    public void setTestPortDOs(TestEntityDO[] testPortDOs) {
        this.testPortDOs = testPortDOs;
    }
}

要模拟的代码:

TestEntityDO[] testEntityMock = testmethod.getTestEntityDO();

我试过的嘲讽:

TestEntityDO[] testEntityDOMock  = PowerMock.createMock(TestEntityDO[].class); // exception is generating at this point
EasyMock.expect(testmethod.getTestEntityDO()).andReturn(testEntityDOMock);

异常跟踪:

java.lang.IllegalArgumentException: Cannot subclass final class class [Lcom.package.TestEntityDO;
    at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at net.sf.cglib.proxy.Enhancer.createClass(Enhancer.java:317)

类不是最终类。仍然将异常指向最终类。 请帮我解决这个问题。

【问题讨论】:

    标签: java junit mocking powermock easymock


    【解决方案1】:

    您正在尝试创建 TestEntityDO 数组的子类/模拟。数组是最终的。

    【讨论】:

    • 那么,什么是模拟的正确方法。在这种情况下?
    • 一组模拟对象。不是模拟的对象数组。
    • 好的.. 我现在应该改变什么?为了工作?那么模拟 Arrays.class 呢?请帮忙
    • 创建一个TestEntityDO[] 类型的数组,并用你需要的模拟TestEntityDO 对象的数量填充它。
    • 但最终我只需要 TestEntityDO[] 模拟。因为后来我必须从 TestEntityDO[] 对象调用许多模拟方法。所以模拟类并添加 TestEntityDO[] 不会模拟 TestEntityDO[] 对象它自己是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    相关资源
    最近更新 更多