I'm trying to test the Pojo classes generated from wsdl to improve code coverage and I'm facing an issue I don't know how to solve.
The generated class looks like this:
public class NOF implements java.io.Serializable {
private java.lang.String nOFName;
public java.lang.String getNOFName() {
return this.nOFName;
}
public void setNOFName(java.lang.String nOFName) {
this.nOFName = nOFName;
}
}
It looks like the Pojo Validator doesn't correctly translate the variable name nOFName
to a getter getNOFName
and looks for something else instead(I don't know what). Exception I get is this:
java.lang.AssertionError: [PojoFieldImpl [field=private java.lang.String
mypackage.NOF.nOFName, fieldGetter=null, fieldSetter=null]] is missing a getter
When I change the name of the variable to NOFName
, the test passes. It also works if I change it to nofName
and rename method to getNofName
. I know the problem is in the name made out of abbreviation, but I really don't want to change the generated classes, because they may be regenerated in the future.
Do I need to write a custom validator? I guess somoene must have dealt with the problem before(because I see this as a perfect use-case for openpojo validation), but I haven't found anything.
The test looks like this:
public class PojoTest {
@Test
public void testPojo() throws Exception{
PojoValidator validator = new PojoValidator();
validator.addRule(new GetterMustExistRule());
validator.addRule(new SetterMustExistRule());
validator.addTester(new GetterTester());
validator.addTester(new SetterTester());
for (PojoClass cls: PojoClassFactory.getPojoClasses("mypackage")){
validator.runValidation(cls);
}
}
}
EDIT
I've debugged the openpojo library and it searches for getnOFName
and there's nothing I can do about it except fix and recompile the library :(
The solution is not very straightforward, but quite easy. I had to mock the Pojo Validation library(I still laugh at that idea). Here's the working code:
@RunWith(PowerMockRunner.class)
@PrepareForTest(AttributeHelper.class)
public class PojoTest {
static PojoValidator validator;
@BeforeClass
public static void setUpClass(){
validator = new PojoValidator();
validator.addRule(new GetterMustExistRule());
validator.addRule(new SetterMustExistRule());
validator.addTester(new GetterTester());
validator.addTester(new SetterTester());
}
@Before
public void setUp(){
PowerMockito.replace(method(AttributeHelper.class, "formattedFieldName")).with(method(PojoTest.class, "formattedFieldName"));
}
@Test
public void testPojo() throws Exception {
for (PojoClass cls: PojoClassFactory.getPojoClasses("mypackage")){
validator.runValidation(cls);
}
}
private static final List<String> fieldPrefixes = new LinkedList<String>();
private static String formattedFieldName(final String fieldName) {
return fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());
}
}
The problem is that the wsdl2java has different interpretation of the Java Beans standard(inferred names), than PojoValidator. I had to replace one static method from the library.