From a648bea0ce87996e395cc29521d0b0d513627dcd Mon Sep 17 00:00:00 2001 From: Johannes Link Date: Sat, 9 Jan 2016 17:39:07 +0100 Subject: [PATCH] Removed unnecessary logic from JUnit4 to 5 runner (JUnit5) ------------------------------------------------------------------------ On behalf of the community, the JUnit Lambda Team thanks Klarna AB (http://www.klarna.com) for supporting the JUnit crowdfunding campaign! ------------------------------------------------------------------------ --- .../org/junit/gen5/junit4/runner/JUnit5.java | 52 ++++++++----------- .../java/com/example/JUnit4SamplesSuite.java | 6 --- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/junit4-runner/src/main/java/org/junit/gen5/junit4/runner/JUnit5.java b/junit4-runner/src/main/java/org/junit/gen5/junit4/runner/JUnit5.java index 59c890d7657..bbab9c8bfab 100644 --- a/junit4-runner/src/main/java/org/junit/gen5/junit4/runner/JUnit5.java +++ b/junit4-runner/src/main/java/org/junit/gen5/junit4/runner/JUnit5.java @@ -14,7 +14,6 @@ import static java.util.stream.Collectors.toList; import static org.junit.gen5.engine.ClassFilters.classNameMatches; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; @@ -32,15 +31,16 @@ import org.junit.runners.model.InitializationError; /** - * JUnit 4 based {@link Runner} which runs tests that use the JUnit 5 - * programming and extension models. + * JUnit 4 based {@link Runner} which runs tests that use the JUnit 5 programming and extension models. * - *

Annotating a test class with {@code @RunWith(JUnit5.class)} allows - * it to be run with IDEs and build systems that support JUnit 4 but do - * not yet support the JUnit 5 APIs directly. + *

Annotating a class with {@code @RunWith(JUnit5.class)} allows it to be run with IDEs and build systems that + * support JUnit 4 but do not yet support the JUnit 5 APIs directly. * - *

Consult the various annotations in this package for configuration - * options. + *

Consult the various annotations in this package for configuration options. + * + *

If you don't use any annotations, you can simply us this runner on a JUnit 5 test class. + * Contrary to standard JUnit 5 test classes, the test class must be {@code public} in order + * to be picked up by IDEs and build tools. * * @since 5.0 * @see Classes @@ -53,8 +53,6 @@ */ public class JUnit5 extends Runner { - public static final String CREATE_SPECIFICATION_METHOD_NAME = "createSpecification"; - private static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; private static final String[] EMPTY_STRING_ARRAY = new String[0]; private static final String EMPTY_STRING = ""; @@ -92,24 +90,8 @@ private JUnit5TestTree generateTestTree(Class testClass) { return new JUnit5TestTree(plan, testClass); } - private TestPlanSpecification createSpecification() throws InitializationError { - try { - Method createSpecMethod = this.testClass.getMethod(CREATE_SPECIFICATION_METHOD_NAME); - return (TestPlanSpecification) createSpecMethod.invoke(null); - } - catch (NoSuchMethodException notUsed) { - return createSpecificationFromAnnotations(); - } - catch (Exception e) { - throw new InitializationError(e); - } - } - - private TestPlanSpecification createSpecificationFromAnnotations() { - List specElements = new ArrayList<>(); - specElements.addAll(getClassSpecificationElements()); - specElements.addAll(getUniqueIdSpecificationElements()); - specElements.addAll(getPackageSpecificationElements()); + private TestPlanSpecification createSpecification() { + List specElements = getSpecElementsFromAnnotations(); // Allows to simply add @RunWith(JUnit5.class) to any JUnit5 test case if (specElements.isEmpty()) { @@ -117,12 +99,24 @@ private TestPlanSpecification createSpecificationFromAnnotations() { } TestPlanSpecification spec = TestPlanSpecification.build(specElements); + addFiltersFromAnnotations(spec); + + return spec; + } + + private void addFiltersFromAnnotations(TestPlanSpecification spec) { addClassNameMatchesFilter(spec); addIncludeTagsFilter(spec); addExcludeTagsFilter(spec); addEngineIdFilter(spec); + } - return spec; + private List getSpecElementsFromAnnotations() { + List specElements = new ArrayList<>(); + specElements.addAll(getClassSpecificationElements()); + specElements.addAll(getUniqueIdSpecificationElements()); + specElements.addAll(getPackageSpecificationElements()); + return specElements; } private List getClassSpecificationElements() { diff --git a/sample-project/src/test/java/com/example/JUnit4SamplesSuite.java b/sample-project/src/test/java/com/example/JUnit4SamplesSuite.java index 019ffcef519..9f21e5f817f 100644 --- a/sample-project/src/test/java/com/example/JUnit4SamplesSuite.java +++ b/sample-project/src/test/java/com/example/JUnit4SamplesSuite.java @@ -26,10 +26,4 @@ @ExcludeTags({ "fast" }) //@OnlyEngine("junit5") public class JUnit4SamplesSuite { - - // When you have the following method, it overrides all annotations - // public static TestPlanSpecification createSpecification() { - // return TestPlanSpecification.build( - // TestPlanSpecification.forNames(SucceedingTestCase.class.getName())); - // } }