Skip to content

Commit

Permalink
Fixed scanning of static inner classes, tests included.
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 11, 2016
1 parent 0743e99 commit b8c0c3d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
Expand Up @@ -259,7 +259,7 @@ public void testPackageResolution() {
"org.junit.gen5.engine.junit5.descriptor.subpackage"); "org.junit.gen5.engine.junit5.descriptor.subpackage");
resolver.resolveElement(specification); resolver.resolveElement(specification);


assertEquals(4, engineDescriptor.allDescendants().size()); assertEquals(6, engineDescriptor.allDescendants().size());
List<String> uniqueIds = uniqueIds(); List<String> uniqueIds = uniqueIds();
assertTrue( assertTrue(
uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.descriptor.subpackage.Class1WithTestCases")); uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.descriptor.subpackage.Class1WithTestCases"));
Expand All @@ -269,6 +269,8 @@ public void testPackageResolution() {
uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.descriptor.subpackage.Class2WithTestCases")); uniqueIds.contains("ENGINE_ID:org.junit.gen5.engine.junit5.descriptor.subpackage.Class2WithTestCases"));
assertTrue(uniqueIds.contains( assertTrue(uniqueIds.contains(
"ENGINE_ID:org.junit.gen5.engine.junit5.descriptor.subpackage.Class2WithTestCases#test2()")); "ENGINE_ID:org.junit.gen5.engine.junit5.descriptor.subpackage.Class2WithTestCases#test2()"));
assertTrue(uniqueIds.contains(
"ENGINE_ID:org.junit.gen5.engine.junit5.descriptor.subpackage.ClassWithStaticInnerTestCases$ShouldBeDiscovered#test1()"));
} }


@Test @Test
Expand Down
@@ -0,0 +1,30 @@
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.gen5.engine.junit5.descriptor.subpackage;

import org.junit.gen5.api.Test;

public class ClassWithStaticInnerTestCases {

static class ShouldBeDiscovered {
@Test
void test1() {

}
}

private static class ShouldNotBeDiscovered {
@Test
void test2() {

}
}
}
Expand Up @@ -23,7 +23,7 @@
import org.junit.gen5.engine.TestPlanSpecificationElement; import org.junit.gen5.engine.TestPlanSpecificationElement;
import org.junit.gen5.engine.TestPlanSpecificationElementVisitor; import org.junit.gen5.engine.TestPlanSpecificationElementVisitor;
import org.junit.gen5.engine.junit5.discovery.IsNestedTestClass; import org.junit.gen5.engine.junit5.discovery.IsNestedTestClass;
import org.junit.gen5.engine.junit5.discovery.IsTestClassWithTests; import org.junit.gen5.engine.junit5.discovery.IsScannableTestClass;
import org.junit.gen5.engine.junit5.discovery.IsTestMethod; import org.junit.gen5.engine.junit5.discovery.IsTestMethod;


/** /**
Expand All @@ -35,7 +35,7 @@ public class SpecificationResolver {


private final IsNestedTestClass isNestedTestClass = new IsNestedTestClass(); private final IsNestedTestClass isNestedTestClass = new IsNestedTestClass();
private final IsTestMethod isTestMethod = new IsTestMethod(); private final IsTestMethod isTestMethod = new IsTestMethod();
private final IsTestClassWithTests isTestClassWithTests = new IsTestClassWithTests(); private final IsScannableTestClass isScannableTestClass = new IsScannableTestClass();


public SpecificationResolver(JUnit5EngineDescriptor engineDescriptor) { public SpecificationResolver(JUnit5EngineDescriptor engineDescriptor) {
this.engineDescriptor = engineDescriptor; this.engineDescriptor = engineDescriptor;
Expand All @@ -61,12 +61,12 @@ public void visitUniqueId(String uniqueId) {


@Override @Override
public void visitPackage(String packageName) { public void visitPackage(String packageName) {
findAllClassesInPackage(packageName, isTestClassWithTests).stream().forEach(this::visitClass); findAllClassesInPackage(packageName, isScannableTestClass).stream().forEach(this::visitClass);
} }


@Override @Override
public void visitAllTests(File rootDirectory) { public void visitAllTests(File rootDirectory) {
ReflectionUtils.findAllClassesInClasspathRoot(rootDirectory, isTestClassWithTests).stream().forEach( ReflectionUtils.findAllClassesInClasspathRoot(rootDirectory, isScannableTestClass).stream().forEach(
this::visitClass); this::visitClass);
} }
}); });
Expand Down
@@ -0,0 +1,34 @@
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

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

import java.util.function.Predicate;

import org.junit.gen5.commons.util.ReflectionUtils;

/**
* Test if a class is a JUnit 5 test class which should be included in package and classpath scanning.
*
* @since 5.0
*/
public class IsScannableTestClass implements Predicate<Class<?>> {

private static final IsTestClassWithTests isTestClassWithTests = new IsTestClassWithTests();

@Override
public boolean test(Class<?> candidate) {
//please do not collapse into single return
if (ReflectionUtils.isPrivate(candidate))
return false;
return isTestClassWithTests.test(candidate);
}

}

0 comments on commit b8c0c3d

Please sign in to comment.