I'm trying to be efficient in writing some Junit tests for my Java Enum classes, and testing the parse() method of each. Here's an example of doing it one at a time for the FileFormatCodeEnum:
@Test
public void testFileFormatCodeEnum() {
FileFormatCodeEnum[] ens = FileFormatCodeEnum.values();
for(FileFormatCodeEnum en : ens) {
Assert.assertEquals(FileFormatCodeEnum.parse(en.getValue()), en);
}
}
Since every Enum class in my project has a parse() method, I'd prefer not to write this same code for every one of them by replacing the class name everywhere it's found.
I'd like to do something more like this:
@Test
public void testAllEnums() {
ArrayList<Class> allEnums = new ArrayList<Class>();
allEnums.add(FileFormatCodeEnum.class);
allEnums.add(RoleTypeCodeEnum.class);
//begin some pseudo-code here
for (Class clazz : allEnums) {
//TODO: get clazz.values() of the enum into an array named 'ens'
for(Generic en : ens) {
//cast the generic enum to a specific enum type maybe?
//then execute the parse method on the specific enum
Assert.assertEquals(SpecificEnum.parse(en.getValue()), en);
}
}
}
I'm not quite sure how to use reflection (?) to achieve this.
Anyone know if there's a simple way?