I am using CodeModel 2.6.
How would I generate this instruction, when the getType( ) method is inherited from an abstract superclass, two levels above the JDefinedClass?
assertEquals(GeneraldocumenMetadata.TYPE, generaldocumenMetadata.getType());
Background:
- The assertEquals(...) method comes from "import static org.junit.Assert.assertEquals;"
- The class hierarchy is
- GeneraldocumenMetadata extends ItemMetadata
- ItemMetadata extends AbstractItemMetadata
- AbstractItemMetadata owns the getType() method, and the private type field
The end-result/desired method is as follows:
@Test
public void testParameterizedConstructorSuccess()
{
String modifiedType = GeneraldocumenMetadata.TYPE + "andMore";
generaldocumenMetadata = new GeneraldocumenMetadata(modifiedType);
assertEquals(modifiedType, generaldocumenMetadata.getType());
}
The CodeModel method looks like this at the moment, however "definedClass.getMethod("getType", new JType[] {}); " is returning null
private void testDefaultConstructorMethod(JFieldVar uutField, JVar staticTYPEVar, final JDefinedClass unitTestClass, JDefinedClass definedClass, JCodeModel codeModel)
{
int modifiers = JMod.PUBLIC;
JMethod unitTestMethod = unitTestClass.method(modifiers, Void.TYPE, "testDefaultConstructor");
unitTestMethod.annotate(org.junit.Test.class);
JBlock unitTestBody = unitTestMethod.body();
unitTestBody.assign(uutField, JExpr._new(unitTestClass));
JClass abstractItemMetadataClass = definedClass._extends();
JMethod getTypeMethod = definedClass.getMethod("getType", new JType[] {});
JExpr.invoke(getTypeMethod);
JInvocation assertEqualsInvoke = codeModel.directClass("org.junit.Assert").staticInvoke("assertEquals").arg(staticTYPEVar).arg(JExpr.invoke(getTypeMethod));
unitTestBody.add(assertEqualsInvoke);
}
You need to invoke the method, instead of getting it as codeModel will not parse your defined class:
which means your codemodel code will look like the following: