Skip to content

Commit 1faa552

Browse files
committed
Classes annotated with @RunWith(Suite.class) do not need to be public.
This fixes a regression in JUnit 4.12 introduced by 1d97da7.
1 parent c85c614 commit 1faa552

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.junit.runners.model.InitializationError;
3131
import org.junit.runners.model.MultipleFailureException;
3232
import org.junit.runners.model.Statement;
33+
import org.junit.validator.PublicClassValidator;
34+
import org.junit.validator.TestClassValidator;
3335

3436
/**
3537
* Implements the JUnit 4 standard test case class model, as defined by the
@@ -56,6 +58,7 @@
5658
* @since 4.5
5759
*/
5860
public class BlockJUnit4ClassRunner extends ParentRunner<FrameworkMethod> {
61+
private static TestClassValidator PUBLIC_CLASS_VALIDATOR = new PublicClassValidator();
5962

6063
private final ConcurrentMap<FrameworkMethod, Description> methodDescriptions = new ConcurrentHashMap<FrameworkMethod, Description>();
6164

@@ -133,13 +136,20 @@ protected List<FrameworkMethod> computeTestMethods() {
133136
protected void collectInitializationErrors(List<Throwable> errors) {
134137
super.collectInitializationErrors(errors);
135138

139+
validatePublicConstructor(errors);
136140
validateNoNonStaticInnerClass(errors);
137141
validateConstructor(errors);
138142
validateInstanceMethods(errors);
139143
validateFields(errors);
140144
validateMethods(errors);
141145
}
142146

147+
private void validatePublicConstructor(List<Throwable> errors) {
148+
if (getTestClass().getJavaClass() != null) {
149+
errors.addAll(PUBLIC_CLASS_VALIDATOR.validateTestClass(getTestClass()));
150+
}
151+
}
152+
143153
protected void validateNoNonStaticInnerClass(List<Throwable> errors) {
144154
if (getTestClass().isANonStaticInnerClass()) {
145155
String gripe = "The inner class " + getTestClass().getName()

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.junit.runners.model.Statement;
4141
import org.junit.runners.model.TestClass;
4242
import org.junit.validator.AnnotationsValidator;
43-
import org.junit.validator.PublicClassValidator;
4443
import org.junit.validator.TestClassValidator;
4544

4645
/**
@@ -58,8 +57,8 @@
5857
*/
5958
public abstract class ParentRunner<T> extends Runner implements Filterable,
6059
Sortable {
61-
private static final List<TestClassValidator> VALIDATORS = Arrays.asList(
62-
new AnnotationsValidator(), new PublicClassValidator());
60+
private static final List<TestClassValidator> VALIDATORS = Arrays.<TestClassValidator>asList(
61+
new AnnotationsValidator());
6362

6463
private final Object childrenLock = new Object();
6564
private final TestClass testClass;

src/test/java/org/junit/tests/running/classes/ParentRunnerTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.junit.tests.running.classes;
22

33
import static org.hamcrest.CoreMatchers.containsString;
4-
import static org.hamcrest.CoreMatchers.equalTo;
54
import static org.hamcrest.CoreMatchers.is;
65
import static org.junit.Assert.assertEquals;
76
import static org.junit.Assert.assertThat;
@@ -137,6 +136,9 @@ public void failWithHelpfulMessageForNonStaticClassRule() {
137136
static class NonPublicTestClass {
138137
public NonPublicTestClass() {
139138
}
139+
140+
@Test
141+
public void alwaysPasses() {}
140142
}
141143

142144
@Test

src/test/java/org/junit/tests/running/classes/SuiteTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.junit.tests.running.classes;
22

3+
import static org.hamcrest.CoreMatchers.containsString;
34
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertThat;
56
import static org.junit.Assert.assertTrue;
@@ -41,6 +42,18 @@ public void fail() {
4142
public static class All {
4243
}
4344

45+
@RunWith(Suite.class)
46+
@SuiteClasses(TestA.class)
47+
static class NonPublicSuite {
48+
}
49+
50+
@RunWith(Suite.class)
51+
@SuiteClasses(TestA.class)
52+
static class NonPublicSuiteWithBeforeClass {
53+
@BeforeClass
54+
public static void doesNothing() {}
55+
}
56+
4457
public static class InheritsAll extends All {
4558
}
4659

@@ -66,6 +79,19 @@ public void suiteTestCountIsCorrect() throws Exception {
6679
assertEquals(2, runner.testCount());
6780
}
6881

82+
@Test
83+
public void suiteClassDoesNotNeedToBePublic() {
84+
JUnitCore core = new JUnitCore();
85+
Result result = core.run(NonPublicSuite.class);
86+
assertEquals(1, result.getRunCount());
87+
assertEquals(0, result.getFailureCount());
88+
}
89+
90+
@Test
91+
public void nonPublicSuiteClassWithBeforeClassFailsWithoutRunningTests() {
92+
assertThat(testResult(NonPublicSuiteWithBeforeClass.class), hasSingleFailureContaining("can not access"));
93+
}
94+
6995
@Test
7096
public void ensureSuitesWorkWithForwardCompatibility() {
7197
junit.framework.Test test = new JUnit4TestAdapter(All.class);

0 commit comments

Comments
 (0)