Skip to content

Commit 0cc4039

Browse files
author
David Saff
committed
Merge pull request #763 from motlin/master
Allow custom test runners to create their own TestClasses and customize the scanning of annotations.
2 parents a14fc5f + dc5f466 commit 0cc4039

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/main/java/org/junit/runners/ParentRunner.java

100644100755
+7-3
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,19 @@ public void finished() {
7676
// do nothing
7777
}
7878
};
79-
79+
8080
/**
8181
* Constructs a new {@code ParentRunner} that will run {@code @TestClass}
8282
*/
8383
protected ParentRunner(Class<?> testClass) throws InitializationError {
84-
fTestClass = new TestClass(testClass);
84+
fTestClass = createTestClass(testClass);
8585
validate();
8686
}
8787

88+
protected TestClass createTestClass(Class<?> testClass) {
89+
return new TestClass(testClass);
90+
}
91+
8892
//
8993
// Must be overridden
9094
//
@@ -218,7 +222,7 @@ private void validateClassRules(List<Throwable> errors) {
218222
* </ol>
219223
* </li>
220224
* </ol>
221-
*
225+
*
222226
* @return {@code Statement}
223227
*/
224228
protected Statement classBlock(final RunNotifier notifier) {

src/main/java/org/junit/runners/model/TestClass.java

100644100755
+8-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public TestClass(Class<?> klass) {
4747
Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations =
4848
new LinkedHashMap<Class<? extends Annotation>, List<FrameworkField>>();
4949

50+
scanAnnotatedMembers(methodsForAnnotations, fieldsForAnnotations);
51+
52+
fMethodsForAnnotations = makeDeeplyUnmodifiable(methodsForAnnotations);
53+
fFieldsForAnnotations = makeDeeplyUnmodifiable(fieldsForAnnotations);
54+
}
55+
56+
protected void scanAnnotatedMembers(Map<Class<? extends Annotation>, List<FrameworkMethod>> methodsForAnnotations, Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations) {
5057
for (Class<?> eachClass : getSuperClasses(fClass)) {
5158
for (Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) {
5259
addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations);
@@ -57,9 +64,6 @@ public TestClass(Class<?> klass) {
5764
addToAnnotationLists(new FrameworkField(eachField), fieldsForAnnotations);
5865
}
5966
}
60-
61-
fMethodsForAnnotations = makeDeeplyUnmodifiable(methodsForAnnotations);
62-
fFieldsForAnnotations = makeDeeplyUnmodifiable(fieldsForAnnotations);
6367
}
6468

6569
private static Field[] getSortedDeclaredFields(Class<?> clazz) {
@@ -72,7 +76,7 @@ public int compare(Field field1, Field field2) {
7276
return declaredFields;
7377
}
7478

75-
private static <T extends FrameworkMember<T>> void addToAnnotationLists(T member,
79+
protected static <T extends FrameworkMember<T>> void addToAnnotationLists(T member,
7680
Map<Class<? extends Annotation>, List<T>> map) {
7781
for (Annotation each : member.getAnnotations()) {
7882
Class<? extends Annotation> type = each.annotationType();

0 commit comments

Comments
 (0)