Skip to content

Commit

Permalink
Removed unnecessary logic from JUnit4 to 5 runner (JUnit5)
Browse files Browse the repository at this point in the history
------------------------------------------------------------------------
On behalf of the community, the JUnit Lambda Team thanks Klarna AB
(http://www.klarna.com) for supporting the JUnit crowdfunding campaign!
------------------------------------------------------------------------
  • Loading branch information
jlink committed Jan 9, 2016
1 parent d98830b commit a648bea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
Expand Up @@ -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;
Expand All @@ -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.
*
* <p>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.
* <p>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.
*
* <p>Consult the various annotations in this package for configuration
* options.
* <p>Consult the various annotations in this package for configuration options.
*
* <p>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
Expand All @@ -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 = "";
Expand Down Expand Up @@ -92,37 +90,33 @@ 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<TestPlanSpecificationElement> specElements = new ArrayList<>();
specElements.addAll(getClassSpecificationElements());
specElements.addAll(getUniqueIdSpecificationElements());
specElements.addAll(getPackageSpecificationElements());
private TestPlanSpecification createSpecification() {
List<TestPlanSpecificationElement> specElements = getSpecElementsFromAnnotations();

// Allows to simply add @RunWith(JUnit5.class) to any JUnit5 test case
if (specElements.isEmpty()) {
specElements.add(TestPlanSpecification.forClass(this.testClass));
}

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<TestPlanSpecificationElement> getSpecElementsFromAnnotations() {
List<TestPlanSpecificationElement> specElements = new ArrayList<>();
specElements.addAll(getClassSpecificationElements());
specElements.addAll(getUniqueIdSpecificationElements());
specElements.addAll(getPackageSpecificationElements());
return specElements;
}

private List<TestPlanSpecificationElement> getClassSpecificationElements() {
Expand Down
Expand Up @@ -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()));
// }
}

0 comments on commit a648bea

Please sign in to comment.