Skip to content

Commit

Permalink
Add classpath scanning support for non-public test methods
Browse files Browse the repository at this point in the history
Fixes #16.
  • Loading branch information
marcphilipp committed Jan 2, 2024
1 parent 72d4f66 commit 0ed189d
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@

package org.junit.support.testng.engine;

import java.util.Arrays;
import java.lang.reflect.Method;
import java.util.List;
import java.util.function.Predicate;

import org.junit.platform.commons.support.HierarchyTraversalMode;
import org.junit.platform.commons.support.ReflectionSupport;

class IsTestNGTestClass implements Predicate<Class<?>> {

@Override
Expand All @@ -22,6 +26,8 @@ public boolean test(Class<?> candidateClass) {
}

private boolean hasMethodWithTestAnnotation(Class<?> candidateClass) {
return Arrays.stream(candidateClass.getMethods()).anyMatch(TestAnnotationUtils::isAnnotatedDirectly);
List<Method> testMethods = ReflectionSupport.findMethods(candidateClass,
TestAnnotationUtils::isAnnotatedDirectly, HierarchyTraversalMode.BOTTOM_UP);
return !testMethods.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,52 @@ void ignoresAnonymousClasses() {
assertThat(rootDescriptor.getChildren()).isEmpty();
}

@Test
void discoversAllTestMethodsForDefaultVisibilityWithDeclaredMethodTestCase() {
var request = request() //
.selectors(selectPackage("example.basics")) //
.filters(includeClassNamePatterns(".+\\.DefaultVisibilityWithDeclaredMethodTestCase")) //
.build();

var rootDescriptor = testEngine.discover(request, engineId);

assertThat(rootDescriptor.getUniqueId()).isEqualTo(engineId);
assertThat(rootDescriptor.getChildren()).hasSize(1);

TestDescriptor classDescriptor = getOnlyElement(rootDescriptor.getChildren());
assertThat(classDescriptor.getDisplayName()).isEqualTo("DefaultVisibilityWithDeclaredMethodTestCase");
assertThat(classDescriptor.getLegacyReportingName()).isEqualTo(
"example.basics.DefaultVisibilityWithDeclaredMethodTestCase");
assertThat(classDescriptor.getType()).isEqualTo(CONTAINER);
assertThat(classDescriptor.getSource()).containsInstanceOf(ClassSource.class);
assertThat(classDescriptor.getChildren()) //
.extracting(TestDescriptor::getDisplayName) //
.containsExactlyInAnyOrder("baseMethod", "declaredMethod");
}

@Test
void discoversAllTestMethodsForDefaultVisibilityWithoutDeclaredMethodTestCase() {
var request = request() //
.selectors(selectPackage("example.basics")) //
.filters(includeClassNamePatterns(".+\\.DefaultVisibilityWithoutDeclaredMethodTestCase")) //
.build();

var rootDescriptor = testEngine.discover(request, engineId);

assertThat(rootDescriptor.getUniqueId()).isEqualTo(engineId);
assertThat(rootDescriptor.getChildren()).hasSize(1);

TestDescriptor classDescriptor = getOnlyElement(rootDescriptor.getChildren());
assertThat(classDescriptor.getDisplayName()).isEqualTo("DefaultVisibilityWithoutDeclaredMethodTestCase");
assertThat(classDescriptor.getLegacyReportingName()).isEqualTo(
"example.basics.DefaultVisibilityWithoutDeclaredMethodTestCase");
assertThat(classDescriptor.getType()).isEqualTo(CONTAINER);
assertThat(classDescriptor.getSource()).containsInstanceOf(ClassSource.class);
assertThat(classDescriptor.getChildren()) //
.extracting(TestDescriptor::getDisplayName) //
.containsExactlyInAnyOrder("baseMethod");
}

interface InterfaceTestCase {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2021 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 v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package example.basics;

import org.testng.annotations.Test;

abstract class DefaultVisibilityBaseTestCase {

@Test
void baseMethod() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2021 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 v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package example.basics;

import org.testng.annotations.Test;

class DefaultVisibilityWithDeclaredMethodTestCase extends DefaultVisibilityBaseTestCase {

@Test
void declaredMethod() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2021 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 v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package example.basics;

class DefaultVisibilityWithoutDeclaredMethodTestCase extends DefaultVisibilityBaseTestCase {
}

0 comments on commit 0ed189d

Please sign in to comment.