Skip to content

Commit

Permalink
TestPlanSpecification can handle method names
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Nov 17, 2015
1 parent 8550e71 commit b45641d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 18 deletions.
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

Expand Down Expand Up @@ -48,18 +49,49 @@ public static TestPlanSpecificationElement forMethod(Class<?> testClass, Method
return new MethodSpecification(testClass, testMethod);
}

public static TestPlanSpecificationElement forClassName(String className) {
Class<?> testClass = ReflectionUtils.loadClass(className).orElseThrow(
() -> new IllegalArgumentException("Class " + className + " not found."));
return forClass(testClass);
public static TestPlanSpecificationElement forName(String anyName) {

Optional<Class<?>> testClassOptional = ReflectionUtils.loadClass(anyName);
if (testClassOptional.isPresent()) {
return forClass(testClassOptional.get());
}

//TODO Handle case when test method is inherited
Optional<Method> testMethodOptional = loadMethod(anyName);
if (testMethodOptional.isPresent()) {
Method testMethod = testMethodOptional.get();
return forMethod(testMethod.getDeclaringClass(), testMethod);
}

throw new IllegalArgumentException(
String.format("'%s' specifies neither a class, nor a method, nor a package.", anyName));
}

//TODO Move to ReflectionUtils and handle parameters
private static Optional<Method> loadMethod(String anyName) {
Optional<Method> testMethodOptional = Optional.empty();
int hashPosition = anyName.lastIndexOf('#');
if (hashPosition >= 0 && hashPosition < anyName.length()) {
String className = anyName.substring(0, hashPosition);
String methodName = anyName.substring(hashPosition + 1);
Optional<Class<?>> methodClassOptional = ReflectionUtils.loadClass(className);
if (methodClassOptional.isPresent()) {
try {
testMethodOptional = Optional.of(methodClassOptional.get().getDeclaredMethod(methodName));
}
catch (NoSuchMethodException ignore) {
}
}
}
return testMethodOptional;
}

public static List<TestPlanSpecificationElement> forClassNames(Collection<String> classNames) {
return forClassNames(classNames.stream());
return forNames(classNames.stream());
}

public static List<TestPlanSpecificationElement> forClassNames(Stream<String> classNames) {
return classNames.map(name -> forClassName(name)).collect(toList());
private static List<TestPlanSpecificationElement> forNames(Stream<String> classNames) {
return classNames.map(name -> forName(name)).collect(toList());
}

public static TestPlanSpecificationElement forClass(Class<?> testClass) {
Expand Down
Expand Up @@ -10,7 +10,7 @@

package org.junit.gen5.engine;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;

import org.junit.Test;

Expand All @@ -30,4 +30,24 @@ public void testPlanBuilderDemo() {
assertNotNull(specification);
}

@Test
public void testForNameWithClass() {
TestPlanSpecificationElement specification = TestPlanSpecification.forName(MyTestClass.class.getName());
assertEquals(ClassSpecification.class, specification.getClass());
}

@Test
public void testForNameWithMethod() throws NoSuchMethodException {
String methodName = MyTestClass.class.getName() + "#" + MyTestClass.class.getDeclaredMethod("myTest").getName();
TestPlanSpecificationElement specification = TestPlanSpecification.forName(methodName);
assertEquals(MethodSpecification.class, specification.getClass());
}
}

class MyTestClass {

@Test
void myTest() {

}
}
Expand Up @@ -209,7 +209,7 @@ private TestPlanSpecification createSpecificationFromAnnotations() {

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

TestPlanSpecification plan = TestPlanSpecification.build(specs);
Expand Down
Expand Up @@ -16,7 +16,7 @@
import static org.junit.gen5.api.Assertions.fail;
import static org.junit.gen5.api.Assumptions.assumeTrue;
import static org.junit.gen5.engine.TestPlanSpecification.build;
import static org.junit.gen5.engine.TestPlanSpecification.forClassName;
import static org.junit.gen5.engine.TestPlanSpecification.forClass;
import static org.junit.gen5.engine.TestPlanSpecification.forUniqueId;

import java.lang.annotation.ElementType;
Expand Down Expand Up @@ -65,7 +65,7 @@ public void init() {
public void executeCompositeTestPlanSpecification() {
TestPlanSpecification spec = build(
forUniqueId("junit5:org.junit.gen5.engine.junit5.JUnit5TestEngineTests$LocalTestCase#alwaysPasses()"),
forClassName(LocalTestCase.class.getName()));
forClass(LocalTestCase.class));

TrackingTestExecutionListener listener = executeTests(spec, 9);

Expand All @@ -78,7 +78,7 @@ public void executeCompositeTestPlanSpecification() {

@org.junit.Test
public void executeTestsForClassName() {
TestPlanSpecification spec = build(forClassName(LocalTestCase.class.getName()));
TestPlanSpecification spec = build(forClass(LocalTestCase.class));
LocalTestCase.countAfterInvoked = 0;

TrackingTestExecutionListener listener = executeTests(spec, 9);
Expand All @@ -94,7 +94,7 @@ public void executeTestsForClassName() {

@org.junit.Test
public void executeTestsWithDisabledTestClass() {
TestPlanSpecification spec = build(forClassName(DisabledTestClassTestCase.class.getName()));
TestPlanSpecification spec = build(forClass(DisabledTestClassTestCase.class));

EngineDescriptor engineDescriptor = discoverTests(spec);
Set<TestDescriptor> descriptors = engineDescriptor.allChildren();
Expand All @@ -114,7 +114,7 @@ public void executeTestsWithDisabledTestClass() {

@org.junit.Test
public void executeTestsWithDisabledTestMethod() {
TestPlanSpecification spec = build(forClassName(DisabledTestMethodTestCase.class.getName()));
TestPlanSpecification spec = build(forClass(DisabledTestMethodTestCase.class));

EngineDescriptor engineDescriptor = discoverTests(spec);
Set<TestDescriptor> descriptors = engineDescriptor.allChildren();
Expand Down Expand Up @@ -162,7 +162,7 @@ public void executeTestForUniqueIdWithExceptionThrownInAfterMethod() {

@org.junit.Test
public void executeTestsForMethodInjectionCases() {
TestPlanSpecification spec = build(forClassName(MethodInjectionTestCase.class.getName()));
TestPlanSpecification spec = build(forClass(MethodInjectionTestCase.class));

EngineDescriptor engineDescriptor = discoverTests(spec);
Assert.assertEquals("# descriptors", 9, engineDescriptor.allChildren().size());
Expand All @@ -180,7 +180,7 @@ public void executeTestsForMethodInjectionCases() {

@org.junit.Test
public void executeTestsForMethodInjectionInBeforeAndAfterMethodsCases() {
TestPlanSpecification spec = build(forClassName(BeforeAndAfterMethodInjectionTestCase.class.getName()));
TestPlanSpecification spec = build(forClass(BeforeAndAfterMethodInjectionTestCase.class));

EngineDescriptor engineDescriptor = discoverTests(spec);
Assert.assertEquals("# descriptors", 2, engineDescriptor.allChildren().size());
Expand All @@ -198,7 +198,7 @@ public void executeTestsForMethodInjectionInBeforeAndAfterMethodsCases() {

@org.junit.Test
public void executeTestsForMethodWithTestDecoratorAnnotation() {
TestPlanSpecification spec = build(forClassName(TestDecoratorOnMethodTestCase.class.getName()));
TestPlanSpecification spec = build(forClass(TestDecoratorOnMethodTestCase.class));

EngineDescriptor engineDescriptor = discoverTests(spec);
Assert.assertEquals("# descriptors", 2, engineDescriptor.allChildren().size());
Expand All @@ -216,7 +216,7 @@ public void executeTestsForMethodWithTestDecoratorAnnotation() {

@org.junit.Test
public void executeTestCaseWithInnerContext() {
TestPlanSpecification spec = build(forClassName(TestCaseWithContext.class.getName()));
TestPlanSpecification spec = build(forClass(TestCaseWithContext.class));

TestCaseWithContext.countAfterInvoked = 0;

Expand Down

0 comments on commit b45641d

Please sign in to comment.