Reputation: 139
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 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);
}
Upvotes: 2
Views: 3637
Reputation: 11113
You need to invoke the method, instead of getting it as codeModel will not parse your defined class:
JExpr.invoke(uutField, "getType");
which means your codemodel code will look like the following:
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();
JInvocation assertEqualsInvoke = codeModel.ref(org.junit.Assert.class).staticInvoke("assertEquals").arg(staticTYPEVar).arg(JExpr.invoke(uutField, "getType"));
unitTestBody.add(assertEqualsInvoke);
}
Upvotes: 2