Skip to content

Commit

Permalink
Classes annotated with @RunWith(Suite.class) do not need to be public.
Browse files Browse the repository at this point in the history
This fixes a regression in JUnit 4.12.
  • Loading branch information
kcooney committed Jan 9, 2017
1 parent c85c614 commit 8958635
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
import org.junit.validator.PublicClassValidator;
import org.junit.validator.TestClassValidator;

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

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

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

validatePublicConstructor(errors);
validateNoNonStaticInnerClass(errors);
validateConstructor(errors);
validateInstanceMethods(errors);
validateFields(errors);
validateMethods(errors);
}

private void validatePublicConstructor(List<Throwable> errors) {
if (getTestClass().getJavaClass() != null) {
errors.addAll(PUBLIC_CLASS_VALIDATOR.validateTestClass(getTestClass()));
}
}

protected void validateNoNonStaticInnerClass(List<Throwable> errors) {
if (getTestClass().isANonStaticInnerClass()) {
String gripe = "The inner class " + getTestClass().getName()
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/junit/runners/ParentRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestClass;
import org.junit.validator.AnnotationsValidator;
import org.junit.validator.PublicClassValidator;
import org.junit.validator.TestClassValidator;

/**
Expand All @@ -58,8 +57,8 @@
*/
public abstract class ParentRunner<T> extends Runner implements Filterable,
Sortable {
private static final List<TestClassValidator> VALIDATORS = Arrays.asList(
new AnnotationsValidator(), new PublicClassValidator());
private static final List<TestClassValidator> VALIDATORS = Arrays.<TestClassValidator>asList(
new AnnotationsValidator());

private final Object childrenLock = new Object();
private final TestClass testClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.junit.tests.running.classes;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -137,6 +136,9 @@ public void failWithHelpfulMessageForNonStaticClassRule() {
static class NonPublicTestClass {
public NonPublicTestClass() {
}

@Test
public void alwaysPasses() {}
}

@Test
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/junit/tests/running/classes/SuiteTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.junit.tests.running.classes;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -41,6 +42,18 @@ public void fail() {
public static class All {
}

@RunWith(Suite.class)
@SuiteClasses(TestA.class)
static class NonPublicSuite {
}

@RunWith(Suite.class)
@SuiteClasses(TestA.class)
static class NonPublicSuiteWithBeforeClass {
@BeforeClass
public static void doesNothing() {}
}

public static class InheritsAll extends All {
}

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

@Test
public void suiteClassDoesNotNeedToBePublic() {
JUnitCore core = new JUnitCore();
Result result = core.run(NonPublicSuite.class);
assertEquals(1, result.getRunCount());
assertEquals(0, result.getFailureCount());
}

@Test
public void nonPublicSuiteClassWithBeforeClassFailsWithoutRunningTests() {
assertThat(testResult(NonPublicSuiteWithBeforeClass.class), hasSingleFailureContaining("can not access"));
}

@Test
public void ensureSuitesWorkWithForwardCompatibility() {
junit.framework.Test test = new JUnit4TestAdapter(All.class);
Expand Down

0 comments on commit 8958635

Please sign in to comment.