Skip to content

Commit

Permalink
Fixes junit-team#1189: JUnit4TestAdapter blocks JUnit-3-style suites …
Browse files Browse the repository at this point in the history
…that are included via @RunWith(Suite.class)
  • Loading branch information
mkeller committed Aug 12, 2015
1 parent 3e43555 commit 7aeb607
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/main/java/junit/framework/JUnit4TestAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
import org.junit.runner.manipulation.Sortable;
import org.junit.runner.manipulation.Sorter;

/**
* The JUnit4TestAdapter enables running JUnit-4-style tests using a JUnit-3-style test runner.
* <p>
* To use it, add the following to a test class:
* <pre>
public static Test suite() {
return new JUnit4TestAdapter(<em>YourJUnit4TestClass</em>.class);
}
</pre>
*/
public class JUnit4TestAdapter implements Test, Filterable, Sortable, Describable {
private final Class<?> fNewTestClass;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ protected JUnit3Builder junit3Builder() {
}

protected AnnotatedBuilder annotatedBuilder() {
return new AnnotatedBuilder(this);
// For compatibility with Request#classes(...) and the Suite runner,
// deeper nesting levels always need to support suite() methods:
RunnerBuilder suiteBuilder = canUseSuiteMethod ? this : new AllDefaultPossibilitiesBuilder(true);
return new AnnotatedBuilder(suiteBuilder);
}

protected IgnoredBuilder ignoredBuilder() {
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/junit/tests/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import org.junit.runner.notification.ConcurrentRunNotifierTest;
import org.junit.runner.notification.RunNotifierTest;
import org.junit.runner.notification.SynchronizedRunListenerTest;
import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersTest;
import org.junit.runners.CustomBlockJUnit4ClassRunnerTest;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.junit.runners.model.FrameworkFieldTest;
import org.junit.runners.model.FrameworkMethodTest;
import org.junit.runners.model.TestClassTest;
import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersTest;
import org.junit.runners.parameterized.ParameterizedNamesTest;
import org.junit.runners.parameterized.TestWithParametersTest;
import org.junit.tests.assertion.AssertionTest;
Expand Down Expand Up @@ -82,6 +82,7 @@
import org.junit.tests.junit3compatibility.ForwardCompatibilityTest;
import org.junit.tests.junit3compatibility.InitializationErrorForwardCompatibilityTest;
import org.junit.tests.junit3compatibility.JUnit38ClassRunnerTest;
import org.junit.tests.junit3compatibility.JUnit4TestAdapterTest;
import org.junit.tests.junit3compatibility.OldTestClassAdaptingListenerTest;
import org.junit.tests.junit3compatibility.OldTests;
import org.junit.tests.junit3compatibility.SuiteMethodTest;
Expand Down Expand Up @@ -157,6 +158,7 @@
UserStopTest.class,
SortableTest.class,
JUnit38ClassRunnerTest.class,
JUnit4TestAdapterTest.class,
SystemExitTest.class,
JUnitCoreReturnsCorrectExitCodeTest.class,
SuiteMethodTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.junit.tests.junit3compatibility;

import static org.junit.Assert.assertEquals;

import java.util.Collections;

import junit.framework.JUnit4TestAdapter;
import junit.framework.TestCase;
import junit.framework.TestResult;
import junit.framework.TestSuite;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

public class JUnit4TestAdapterTest {

private static void doTest(Class<?> clazz) {
// JUnit 4 runner:
Result result = JUnitCore.runClasses(clazz);
assertEquals(1, result.getRunCount());
assertEquals(0, result.getFailureCount());
assertEquals(0, result.getIgnoreCount());

// JUnit 3 runner:
TestResult testResult = new TestResult();
new JUnit4TestAdapter(clazz).run(testResult);
assertEquals(1, testResult.runCount());
assertEquals(0, testResult.failureCount());
assertEquals(Collections.emptyList(), Collections.list(testResult.errors()));
}

public static class Test4 {
@Test
public void pass() throws Exception {
//pass
}
}

@RunWith(Suite.class)
@Suite.SuiteClasses(Test4.class)
public static class TestSuiteFor4 {
}

@Test
public void test4() {
doTest(TestSuiteFor4.class);
}

public static class Test3 extends TestCase {
public void testPass() throws Exception {
//pass
}
}

@RunWith(Suite.class)
@Suite.SuiteClasses(Test3.class)
public static class TestSuiteFor3 {
}

@Test
public void test3() {
doTest(TestSuiteFor3.class);
}

public static class TestSuite3 {
public static junit.framework.Test suite() {
return new TestSuite(Test3.class);
}
}

@RunWith(Suite.class)
@Suite.SuiteClasses(TestSuite3.class)
public static class TestSuite4ForTestSuite3 {
}

/**
* Test a JUnit-4-style test suite that contains a test
* class that is a JUnit-3-style test suite.
*/
@Test
public void testSuite4ForTestSuite3() {
doTest(TestSuite4ForTestSuite3.class);
}
}

0 comments on commit 7aeb607

Please sign in to comment.