Skip to content

Commit

Permalink
Remove hard-coded test finder from JUnitCore
Browse files Browse the repository at this point in the history
The test finder should be obtained by looking into the container and
determining what kind of tests can be run there.
Rename JUnit4TestFinderTest to JUnitTestFinderTest and make it a
parameterized test that runs both in JUnit4 and JUnit5 mode and add a
regression test to it ("testInnerClassWithNestedAnnotationIsFound")

Fixes #952
  • Loading branch information
fedejeanne committed Dec 8, 2023
1 parent fb32f66 commit 4823400
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
import org.eclipse.jdt.core.IType;

import org.eclipse.jdt.internal.junit.JUnitCorePlugin;
import org.eclipse.jdt.internal.junit.launcher.JUnit4TestFinder;
import org.eclipse.jdt.internal.junit.launcher.ITestFinder;
import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry;
import org.eclipse.jdt.internal.junit.model.JUnitModel;
import org.eclipse.jdt.internal.junit.model.ModelMessages;
import org.eclipse.jdt.internal.junit.model.TestRunSession;
Expand Down Expand Up @@ -156,7 +157,8 @@ public static void removeTestRunListener(TestRunListener listener) {
*/
public static IType[] findTestTypes(IJavaElement container, IProgressMonitor monitor) throws CoreException, OperationCanceledException {
final Set<IType> result= new HashSet<>();
JUnit4TestFinder finder= new JUnit4TestFinder();
ITestFinder finder= TestKindRegistry.getContainerTestKind(container).getFinder();

finder.findTestsInContainer(container, result, monitor);

return result.toArray(new IType[result.size()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
Expand Down Expand Up @@ -43,7 +42,9 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

import org.eclipse.jdt.internal.junit.launcher.ITestFinder;
import org.eclipse.jdt.internal.junit.launcher.JUnit5TestFinder;
import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry;


/**
Expand Down Expand Up @@ -111,6 +112,14 @@ public static void afterClass() throws Exception {
JavaProjectHelper.delete(javaProject);
}

@Test
public void testTestKindRegistryGetContainerTestKind_isJUnit5TestFinder() throws Exception {
IType type= findTypeWithName(scenario.testClass());

ITestFinder finder= TestKindRegistry.getContainerTestKind(type).getFinder();
assertThat(finder).isInstanceOf(JUnit5TestFinder.class);
}

@Test
public void testFindTestsInContainer() throws Exception {
IType type= findTypeWithName(scenario.testClass());
Expand All @@ -131,14 +140,8 @@ private IType findTypeWithName(String name) throws JavaModelException {
return null;
}

private static ICompilationUnit createCompilationUnit(IPackageFragment packageFragment) throws IOException {
private static ICompilationUnit createCompilationUnit(IPackageFragment packageFragment) throws Exception {
String content= Files.readString(TEST_CLASS_FILE);

try {
return packageFragment.createCompilationUnit(JAVA_FILE_NAME, content, false, null);
} catch (JavaModelException e) {
// let the test fail
throw new RuntimeException(e);
}
return packageFragment.createCompilationUnit(JAVA_FILE_NAME, content, false, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
TestRunSessionSerializationTests4.class,

JUnit3TestFinderTest.class,
JUnit4TestFinderTest.class,
JUnitTestFinderTest.class,
JUnit4TestFinderTest16.class,
JUnit5TestFinderJupiterTest.class,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,27 @@

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.function.Consumer;

import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import org.eclipse.jdt.junit.JUnitCore;
import org.eclipse.jdt.testplugin.JavaProjectHelper;
import org.eclipse.jdt.testplugin.JavaTestPlugin;
import org.eclipse.jdt.testplugin.StringAsserts;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

import org.eclipse.jdt.core.IClasspathEntry;
Expand All @@ -44,18 +53,36 @@
import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry;


public class JUnit4TestFinderTest {
@RunWith(Parameterized.class)
public class JUnitTestFinderTest {
private static record TestScenario(String name, IPath containerEntry, Consumer<IJavaProject> setCompilerOptions, String testKindId) {

@Override
public String toString() {
return name;
}
}

private IJavaProject fProject;
private IPackageFragmentRoot fRoot;

@Parameters(name = "{0}")
public static Collection<TestScenario> getTestScenarios() {
return List.of(new TestScenario("JUnit4", JUnitCore.JUNIT4_CONTAINER_PATH, JavaProjectHelper::set15CompilerOptions, TestKindRegistry.JUNIT4_TEST_KIND_ID), //
new TestScenario("JUnit5", JUnitCore.JUNIT5_CONTAINER_PATH, JavaProjectHelper::set18CompilerOptions, TestKindRegistry.JUNIT5_TEST_KIND_ID));
}

@Parameter
public TestScenario fScenario;

@Before
public void setUp() throws Exception {
fProject= JavaProjectHelper.createJavaProject("TestProject", "bin");
JavaProjectHelper.addRTJar(fProject);
IClasspathEntry cpe= JavaCore.newContainerEntry(JUnitCore.JUNIT4_CONTAINER_PATH);
IClasspathEntry cpe= JavaCore.newContainerEntry(fScenario.containerEntry());
JavaProjectHelper.addToClasspath(fProject, cpe);
JavaProjectHelper.set15CompilerOptions(fProject);

fScenario.setCompilerOptions().accept(fProject);

fRoot= JavaProjectHelper.addSourceContainer(fProject, "src");
}
Expand Down Expand Up @@ -127,6 +154,8 @@ public void testTestCase() throws Exception {
assertTestFound(validTest4, new String[] { "p.Outer.InnerTest" });
assertTestFound(validTest4.getCompilationUnit(), new String[] { "p.Outer.InnerTest" });

// Only private classes are invisible for JUnit5
String innerClassVisibility= TestKindRegistry.JUNIT5_TEST_KIND_ID.equals(fScenario.testKindId()) ? "private" : "";
buf= new StringBuilder();
buf.append("package p;\n");
buf.append("import junit.framework.TestCase;\n");
Expand All @@ -136,7 +165,7 @@ public void testTestCase() throws Exception {
buf.append(" public void testFoo() {\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append(" static class NonVisibleInnerTest extends TestCase {\n");
buf.append(" " + innerClassVisibility + " static class NonVisibleInnerTest extends TestCase {\n");
buf.append(" public void testFoo() {\n");
buf.append(" class LocalTest extends TestCase {\n");
buf.append(" public void testFoo() {\n");
Expand Down Expand Up @@ -447,10 +476,34 @@ public void testTestAnnotation2() throws Exception {
assertTestFound(validTest1.getCompilationUnit(), new String[] { "p.Test1" });
}

@Test
public void testInnerClassWithNestedAnnotationIsFound() throws Exception {

Assume.assumeTrue("@Nested only works with JUnit5", fScenario.testKindId().equals(TestKindRegistry.JUNIT5_TEST_KIND_ID));

IPackageFragment p= fRoot.createPackageFragment("p", true, null);
String content= """
package p;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Nested;
class Test1 {
@Nested class NestedClass {
@Test void myTest() {}
}
}
""";

IType validTest1= p.createCompilationUnit("Test1.java", content, false, null).getType("Test1");

assertTestFound(validTest1, new String[] { "p.Test1" });
assertTestFound(validTest1.getCompilationUnit(), new String[] { "p.Test1", "p.Test1.NestedClass" });
}

private void assertTestFound(IJavaElement container, String[] expectedTypes) throws CoreException {
ITestKind testKind= TestKindRegistry.getContainerTestKind(container);
assertEquals(TestKindRegistry.JUNIT4_TEST_KIND_ID, testKind.getId());
assertEquals(fScenario.testKindId(), testKind.getId());

ITestFinder finder= testKind.getFinder();

Expand Down

0 comments on commit 4823400

Please sign in to comment.