From 82597c08954d308573ea7a86f27619eef8bdbfa6 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 4 Dec 2015 14:56:12 +0100 Subject: [PATCH] Expand test coverage of TestPlanSpecification --- .../gen5/engine/TestPlanSpecification.java | 6 +- .../engine/TestPlanSpecificationTests.java | 59 +++++++++++++------ 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/junit-engine-api/src/main/java/org/junit/gen5/engine/TestPlanSpecification.java b/junit-engine-api/src/main/java/org/junit/gen5/engine/TestPlanSpecification.java index 7609fbcb1ef..fa62ea7895c 100644 --- a/junit-engine-api/src/main/java/org/junit/gen5/engine/TestPlanSpecification.java +++ b/junit-engine-api/src/main/java/org/junit/gen5/engine/TestPlanSpecification.java @@ -136,7 +136,11 @@ public TestPlanSpecification(List elements) { @Override public Iterator iterator() { - return unmodifiableList(this.elements).iterator(); + return getElements().iterator(); + } + + List getElements() { + return unmodifiableList(this.elements); } public void accept(TestPlanSpecificationElementVisitor visitor) { diff --git a/junit-tests/src/test/java/org/junit/gen5/engine/TestPlanSpecificationTests.java b/junit-tests/src/test/java/org/junit/gen5/engine/TestPlanSpecificationTests.java index a304b1dfa3f..fe0ad2f0892 100644 --- a/junit-tests/src/test/java/org/junit/gen5/engine/TestPlanSpecificationTests.java +++ b/junit-tests/src/test/java/org/junit/gen5/engine/TestPlanSpecificationTests.java @@ -10,8 +10,15 @@ package org.junit.gen5.engine; +import static java.util.stream.Collectors.toList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.gen5.engine.TestPlanSpecification.build; +import static org.junit.gen5.engine.TestPlanSpecification.forName; +import static org.junit.gen5.engine.TestPlanSpecification.forUniqueId; + +import java.util.Arrays; +import java.util.List; import org.junit.Test; @@ -23,38 +30,54 @@ public class TestPlanSpecificationTests { @Test - public void testPlanBuilderDemo() { - TestPlanSpecification specification = TestPlanSpecification.build( - TestPlanSpecification.forUniqueId("junit5:org.example.UserTests#fullname()")); + public void forUniqueIdForMethod() { + TestPlanSpecificationElement element = forUniqueId("junit5:org.example.UserTests#fullname()"); + assertEquals(UniqueIdSpecification.class, element.getClass()); + } - assertNotNull(specification); + @Test + public void forNameWithClass() { + TestPlanSpecificationElement element = forName(MyTestClass.class.getName()); + assertEquals(ClassSpecification.class, element.getClass()); } @Test - public void testForNameWithClass() { - TestPlanSpecificationElement specification = TestPlanSpecification.forName(MyTestClass.class.getName()); - assertEquals(ClassSpecification.class, specification.getClass()); + public void forNameWithMethod() throws Exception { + TestPlanSpecificationElement element = forName(fullyQualifiedMethodName()); + assertEquals(MethodSpecification.class, element.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()); + public void forNameWithPackage() { + TestPlanSpecificationElement element = forName("org.junit.gen5"); + assertEquals(PackageSpecification.class, element.getClass()); } @Test - public void testForNameWithPackage() throws NoSuchMethodException { - String packageName = "org.junit.gen5"; - TestPlanSpecificationElement specification = TestPlanSpecification.forName(packageName); - assertEquals(PackageSpecification.class, specification.getClass()); + public void buildSpecification() throws Exception { + // @formatter:off + TestPlanSpecification spec = build( + forUniqueId("junit5:org.example.UserTests#fullname()"), + forName(MyTestClass.class.getName()), + forName("org.junit.gen5"), + forName(fullyQualifiedMethodName()) + ); + // @formatter:on + + assertNotNull(spec); + List> expected = Arrays.asList(UniqueIdSpecification.class, + ClassSpecification.class, PackageSpecification.class, MethodSpecification.class); + assertEquals(expected, spec.getElements().stream().map(Object::getClass).collect(toList())); } - class MyTestClass { + private String fullyQualifiedMethodName() throws Exception { + return MyTestClass.class.getName() + "#" + MyTestClass.class.getDeclaredMethod("myTest").getName(); + } - @Test - void myTest() { + static class MyTestClass { + void myTest() { } } + }