Skip to content

Commit

Permalink
#58 Added dynamic tests to classpath/package scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Mar 4, 2016
1 parent e7bac54 commit 8624c1d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions documentation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ install.enabled = false

junit5 {
runJunit4 true
excludeTag 'exclude'
logManager 'org.apache.logging.log4j.jul.LogManager'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package example;

import org.junit.gen5.junit4.runner.ExcludeTags;
import org.junit.gen5.junit4.runner.JUnit5;
import org.junit.gen5.junit4.runner.Packages;
import org.junit.runner.RunWith;
Expand All @@ -29,5 +30,6 @@
*/
@RunWith(JUnit5.class)
@Packages("example")
@ExcludeTags("exclude")
public class DocumentationTestSuite {
}
2 changes: 2 additions & 0 deletions documentation/src/test/java/example/DynamicTestsDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import org.junit.gen5.api.Assertions;
import org.junit.gen5.api.Dynamic;
import org.junit.gen5.api.DynamicTest;
import org.junit.gen5.api.Tag;
import org.junit.gen5.junit4.runner.JUnit5;
import org.junit.runner.RunWith;

@RunWith(JUnit5.class)
@Tag("exclude")
public class DynamicTestsDemo {

// @Dynamic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@

package org.junit.gen5.engine.junit5.discovery;

import static org.junit.gen5.api.Assertions.assertEquals;
import static org.junit.gen5.api.Assertions.assertSame;
import static org.junit.gen5.api.Assertions.assertThrows;
import static org.junit.gen5.api.Assertions.assertTrue;
import static org.junit.gen5.api.Assertions.expectThrows;
import static org.junit.gen5.api.Assertions.*;
import static org.junit.gen5.launcher.main.TestDiscoveryRequestBuilder.request;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.gen5.api.Dynamic;
import org.junit.gen5.api.DynamicTest;
import org.junit.gen5.api.Nested;
import org.junit.gen5.api.Test;
import org.junit.gen5.commons.util.PreconditionViolationException;
Expand All @@ -41,11 +41,12 @@ public void testSingleClassResolution() {

resolver.resolveSelectors(request().select(selector).build());

assertEquals(3, engineDescriptor.allDescendants().size());
assertEquals(4, engineDescriptor.allDescendants().size());
List<String> uniqueIds = uniqueIds();
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.MyTestClass"));
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.MyTestClass#test1()"));
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.MyTestClass#test2()"));
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.MyTestClass#dynamicTest()"));
}

@Test
Expand All @@ -55,12 +56,13 @@ public void testTwoClassesResolution() {

resolver.resolveSelectors(request().select(selector1, selector2).build());

assertEquals(6, engineDescriptor.allDescendants().size());
assertEquals(7, engineDescriptor.allDescendants().size());
List<String> uniqueIds = engineDescriptor.allDescendants().stream().map(d -> d.getUniqueId()).collect(
Collectors.toList());
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.MyTestClass"));
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.MyTestClass#test1()"));
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.MyTestClass#test2()"));
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.MyTestClass#test2()"));
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.YourTestClass"));
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.YourTestClass#test3()"));
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.YourTestClass#test4()"));
Expand Down Expand Up @@ -126,11 +128,12 @@ public void testClassResolutionByUniqueId() {

resolver.resolveSelectors(request().select(selector).build());

assertEquals(3, engineDescriptor.allDescendants().size());
assertEquals(4, engineDescriptor.allDescendants().size());
List<String> uniqueIds = uniqueIds();
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.MyTestClass"));
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.MyTestClass#test1()"));
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.MyTestClass#test2()"));
assertTrue(uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.discovery.MyTestClass#test2()"));
}

@Test
Expand Down Expand Up @@ -410,6 +413,11 @@ void test2() {
void notATest() {

}

@Dynamic
Stream<DynamicTest> dynamicTest() {
return new ArrayList<DynamicTest>().stream();
}
}

class YourTestClass {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
public class IsTestClassWithTests implements Predicate<Class<?>> {

private static final IsTestMethod isTestMethod = new IsTestMethod();
private static final IsDynamicTestMethod isDynamicTestMethod = new IsDynamicTestMethod();

private static final IsPotentialTestContainer isPotentialTestContainer = new IsPotentialTestContainer();

Expand All @@ -37,14 +38,17 @@ public boolean test(Class<?> candidate) {
//please do not collapse into single return
if (!isPotentialTestContainer.test(candidate))
return false;
return hasTestMethods(candidate) || hasNestedTests(candidate);
return hasTestMethods(candidate) || hasDynamicTests(candidate) || hasNestedTests(candidate);
}

private boolean hasTestMethods(Class<?> candidate) {
//Todo: Add check for dynamic tests (IsDynamicTestMethod)
return !ReflectionUtils.findMethods(candidate, isTestMethod, HierarchyDown).isEmpty();
}

private boolean hasDynamicTests(Class<?> candidate) {
return !ReflectionUtils.findMethods(candidate, isDynamicTestMethod, HierarchyDown).isEmpty();
}

private boolean hasNestedTests(Class<?> candidate) {
return !ReflectionUtils.findNestedClasses(candidate, isNestedTestClass).isEmpty();
}
Expand Down

0 comments on commit 8624c1d

Please sign in to comment.