[junit] ensure pattern configuration starts as junit 4 if all parts are not higher than junit 4

- do not start as junit 5 because this might break the order of classes in the configuration

^IDEA-381486 fixed

GitOrigin-RevId: 3cc3f8af552f6bb9b80652a5e811ddd0e45876a6
This commit is contained in:
Anna Kozlova
2025-10-31 12:19:18 +00:00
committed by intellij-monorepo-bot
parent 0b51ffb0d1
commit 5e4f00cf18
4 changed files with 41 additions and 0 deletions
@@ -194,6 +194,25 @@ public class JUnitConfigurationTest extends JUnitConfigurationTestCase {
lines);
}
public void testPatternConfigurationWith() throws ExecutionException, IOException {
VirtualFile module1Content = findFile("moduleWithBothVersions");
createModule(module1Content, true, "JUnit5");
Module module = getModule(3);
IntelliJProjectConfiguration.LibraryRoots junit4Library = IntelliJProjectConfiguration.getProjectLibrary("JUnit4");
ModuleRootModificationUtil.addModuleLibrary(module, "JUnit4", junit4Library.getClassesUrls(), junit4Library.getSourcesUrls());
JUnitConfiguration configuration = new JUnitConfiguration("", myProject);
configuration.getPersistentData().TEST_OBJECT = JUnitConfiguration.TEST_PATTERN;
configuration.getPersistentData().setPatterns(ContainerUtil.newLinkedHashSet("with4.TestCaseInheritor", "with4.TestCaseInheritorMultipleConstructor"));
configuration.setModule(module);
JavaParameters parameters = checkCanRun(configuration);
ParametersList parametersList = parameters.getProgramParametersList();
assertTrue("Expected to run as junit4 but program parameters are: " + parametersList.getParametersString(), parametersList.getList().contains("-junit4"));
}
public void testRunningAllInPackage() throws IOException, ExecutionException {
Module module1 = getModule1();
GlobalSearchScope module1AndLibraries = GlobalSearchScope.moduleWithLibrariesScope(module1);
@@ -0,0 +1,4 @@
package with4;
public class TestCaseInheritor extends junit.framework.TestCase {
public void test1() {}
}
@@ -0,0 +1,5 @@
package with4;
public class TestCaseInheritorMultipleConstructor extends junit.framework.TestCase {
public TestCaseInheritorMultipleConstructor(String s) {}
public void test1() {}
}
@@ -816,6 +816,19 @@ public abstract class TestObject extends JavaTestFrameworkRunnableState<JUnitCon
}
}
if (JUnitConfiguration.TEST_PATTERN.equals(data.TEST_OBJECT)) {
if (ContainerUtil.and(data.getPatterns(), name -> {
PsiClass aClass = JavaExecutionUtil.findMainClass(project, name, globalSearchScope);
if (aClass == null) {
return false;
}
TestFramework framework = TestFrameworks.detectFramework(aClass);
return framework instanceof JUnit4Framework || framework instanceof JUnit3Framework;
})) {
return JUnitStarter.JUNIT4_PARAMETER;
}
}
return getRunner(globalSearchScope, project);
}