6

im trying to Mock below class.

 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;
    }
}

Code to be Mocked:

TestEntityDO[] testEntityMock = testmethod.getTestEntityDO();

Mocking i tried:

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

exception trace:

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)

class is not a final class. still the exception is pointed as final class. please help me to solve this issue.

Manjunath
  • 111
  • 1
  • 8

1 Answers1

6

You're trying to create a subclass/mock of an array of TestEntityDO. Arrays are final.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • So, what is the right way to mock. in this scenario? – Manjunath Oct 25 '15 at 17:34
  • An array of mocked objects. Not a mocked array of objects. – Kayaman Oct 25 '15 at 17:37
  • ok.. what should i have to change now? in order to work? mock Arrays.class then? please help – Manjunath Oct 25 '15 at 17:42
  • Create an array of type `TestEntityDO[]` and fill it with the amount of mocked `TestEntityDO` objects you need. – Kayaman Oct 25 '15 at 17:44
  • but ultimately i need TestEntityDO[] mock only. because later i have to call many mocked methods from TestEntityDO[] object. so mocking class and adding in TestEntityDO[] wont be mocking of TestEntityDO[] object it self right. – Manjunath Oct 25 '15 at 18:09
  • No you don't, and even if you did, you can't. You're confusing an array object and your DO objects (which you want to mock). You can't mock an array, but you can fill an array with mock objects. – Kayaman Oct 25 '15 at 18:12
  • but later i need to mock some methods of TestEntityDO[] object it self. like testEntityDOMock.getCapabilities() etc.., how can i achieve this without mocking TestEntityDO[] or is there is any other way? – Manjunath Oct 25 '15 at 18:15
  • Which methods? Arrays don't have too many methods besides the obvious `toString()`, `hashCode()` and `getClass()` ones. – Kayaman Oct 25 '15 at 18:17