Skip to content
Browse files

Changed Enclosed runner to ignore abstract classes. Before this chang…

…e, an abstract enclosed class caused jUnit to throw an exception.
  • Loading branch information...
1 parent e8b91fa commit 0c458a8796cc5ffcda5286ee4a367c8e31ae7c88 @nicobn committed
View
18 src/main/java/org/junit/experimental/runners/Enclosed.java
@@ -1,5 +1,9 @@
package org.junit.experimental.runners;
+import java.lang.reflect.Modifier;
+import java.util.HashSet;
+import java.util.Set;
+
import org.junit.runners.Suite;
import org.junit.runners.model.RunnerBuilder;
@@ -26,6 +30,18 @@
* Only called reflectively. Do not use programmatically.
*/
public Enclosed(Class<?> klass, RunnerBuilder builder) throws Throwable {
- super(builder, klass, klass.getClasses());
+ super(builder, klass, filterOutAbstractClasses(klass.getClasses()));
}
+
+ private static Class<?>[] filterOutAbstractClasses(final Class<?>[] classes) {

filterAbstractClasses() is probably a more succinct name?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ final Set<Class<?>> filteredSet = new HashSet<Class<?>>();
+
+ for (final Class<?> clazz : classes) {
+ if (!Modifier.isAbstract(clazz.getModifiers())) {
+ filteredSet.add(clazz);
+ }
+ }
+
+ return filteredSet.toArray(new Class<?>[] { });

Better to write:

return filteredSet.toArray(new Class<?>[filteredSet.size()]);

Otherwise a new array has to be instantiated using reflection, rather than just using the one you've passed in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
}
View
7 src/test/java/org/junit/tests/running/classes/EnclosedTest.java
@@ -21,14 +21,17 @@
@Test public void b() {}
@Test public void c() {}
}
+ abstract public static class C {
+ @Test public void a() {}
+ }
}
- @Test public void enclosedRunnerPlansEnclosedClasses() throws Exception {
+ @Test public void enclosedRunnerPlansConcreteEnclosedClasses() throws Exception {
Runner runner= Request.aClass(Enclosing.class).getRunner();
assertEquals(5, runner.testCount());
}
- @Test public void enclosedRunnerRunsEnclosedClasses() throws Exception {
+ @Test public void enclosedRunnerRunsConcreteEnclosedClasses() throws Exception {
Result result= JUnitCore.runClasses(Enclosing.class);
assertEquals(5, result.getRunCount());
}

0 comments on commit 0c458a8

Please sign in to comment.
Something went wrong with that request. Please try again.