KentBeck/junit — Issue #359: filtering by category can lead to ExceptionInInitializerError
Author: nkeywal
Created: 2011-11-08 00:15:09+00:00
State: open

I am testing the support of categories in maven/surefire.

In categories.Categories#shouldRun you end up calling Description#getTestClass to check the annotations.

This is implemented by a direct classForName(className). This leads to a full initialization of this class (static & so on), even if at the end this test class won't be retained. As the initialization can be expensive for some categories of tests, it's already an issue.

In a context where there is a separation between the jvm used to select/launch the tests and the jvm used to execute them, it just does not work.

This can be very easily fixed in JUnit by not initializing the class in Description#getTestClass by calling
```Class.forName(name, false, getClass().getClassLoader());```

instead of
```Class.forName(name);```

This method exists since java 1.2 (see http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#forName%28java.lang.String,%20boolean,%20java.lang.ClassLoader%29)

Tested, it works with maven/surefire.