December 15, 2003

Test coverage addict

Since I requested a clover license for nyx / xulux I got hooked to have my coverage to 100%.
The positive part is that I have another good look at my code from a different perspective and fixed numerous possible issues that could arise when other people start writing extensions (eg a gui driver), on the other hand I probably overdo it a bit. Eg I have an imageloader system in place that by default tries to use jimi for showing icons and images (hey it supports ico files). In the interface I have a method isUsable() that checks if the main jimi class is actually is loadable.

public boolean isUsable() {
try {
Class.forName("com.sun.jimi.core.Jimi");
} catch (ClassNotFoundException cnfe) {
return false;
}
return true;
}

Hmm.. How to test the classNotFound exception in this case when jimi is always on your classpath ? You have to write a classloader that throws that exception when it tries to load the jimi class.. (see the JimiImageLoaderTest)
Also my ClassLoaderUtilsTest was a horror to get to 100%..

Maybe some nice junit extensions could be created which handle this behaviour came to mind several times last night..

Have I lost it, or am I actually doing the right thing ?

Posted by mvdb at December 15, 2003 02:23 PM
Comments
How about extracting a generic isUsable that takes a class name as an argument. Then you can test both the passing and failing conditions by passing in valid and invalid class names. Posted by: Jeff Waltzer on December 15, 2003 03:02 PM
Hmm I have to dig into my cvs history for this a bit.. I think I had it generic before, but there was an issue with it, so I changed it. But since a generic solution is better than this approach, i will take some time to investigate.. Thanx for the input. Posted by: Martin van den Bemt on December 15, 2003 03:27 PM
Class.forName() is evil http://radio.weblogs.com/0112098/stories/2003/02/12/classfornameIsEvil.html Refactor to use a ClassLoader - then you can mock it to throw ClassNotFoundException Posted by: James Strachan on December 18, 2003 06:35 AM
Thanx for the reminder James. Had some of that code in there, but they all moved to a util class, which uses the forname thing, so changing that currently :) Posted by: Martin on December 19, 2003 02:54 AM
Post a comment