diff --git a/README.md b/README.md index 79001f6..b4d1516 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ own `@ContextConfiguration` or `@Import` of spring beans. `Spring Boot` 2.4+, 3.x as well as bare Spring framework Supported test frameworks: -* `JUnit 4` (via JUnit 5 [junit-vintage-engine](https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4-running)) +* `JUnit 4` (removed; for JUnit 4 support use old 0.9 version) * `JUnit 5 Jupiter` * `TestNG` (both bare TestNG and JUnit platform [testng-engine](https://github.com/junit-team/testng-engine)) @@ -128,71 +128,8 @@ parent.
JUnit 4 -Note: support of JUnit 4 is planned to be removed in version 1.0 (will stay available in 0.x versions). - -The JUnit 4 does not provide standard way to reorder test class execution, but it's still possible via -[junit-vintage-engine](https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4-running). -This dependency should be added to test scope of the module: -```xml - - org.junit.vintage - junit-vintage-engine - test - -``` -or for Gradle (see [detailed instruction](https://docs.gradle.org/current/userguide/java_testing.html#executing_legacy_tests_with_junit_vintage)): -```groovy -testRuntimeOnly('org.junit.vintage:junit-vintage-engine') -testRuntimeOnly('org.junit.platform:junit-platform-launcher') -``` -Also the `surefire`/`failsafe` plugins should be configured to use junit-platform: -```xml - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire.version} - - - org.apache.maven.surefire - surefire-junit-platform - ${maven-surefire.version} - - - - - org.apache.maven.plugins - maven-failsafe-plugin - ${maven-surefire.version} - - - org.apache.maven.surefire - surefire-junit-platform - ${maven-surefire.version} - - - -``` -or for Gradle: -```groovy -tasks.named('test', Test) { - useJUnitPlatform() -} -``` - -For projects with JUnit 4 it will automatically setup -[SmartDirtiesPostDiscoveryFilter](spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesPostDiscoveryFilter.java) -which will reorder test classes on the level of junit-launcher and prepare the list of last test class per context configuration. -Then this test execution listener -[SmartDirtiesContextTestExecutionListener](spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesContextTestExecutionListener.java) -will be auto-discovered via [spring.factories](spring-test-smart-context/src/main/resources/META-INF/spring.factories). -Alternatively it can be defined explicitly -```java -@TestExecutionListeners(listeners = { - SmartDirtiesContextTestExecutionListener.class -}, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS) -``` -or even inherited from -[AbstractJUnit4SpringIntegrationTest](spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/junit4/AbstractJUnit4SpringIntegrationTest.java) +Note: support of JUnit 4 is removed in version 1.0. +In case you need it, check the 0.9 [README](https://github.com/seregamorph/spring-test-smart-context/blob/v0.9/README.md).
### Additional materials diff --git a/demo/demo-maven-junit-platform-junit4-boot24/pom.xml b/demo/demo-maven-junit-platform-junit4-boot24/pom.xml deleted file mode 100644 index c6dd627..0000000 --- a/demo/demo-maven-junit-platform-junit4-boot24/pom.xml +++ /dev/null @@ -1,105 +0,0 @@ - - - 4.0.0 - - - com.github.seregamorph - demo-parent - 0.10-SNAPSHOT - - - demo-maven-junit-platform-junit4-boot24 - - Demo project with tests launched via Maven junit-platform junit4 vintage engine - - - 2.4.2 - true - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - org.apache.maven.surefire - surefire-junit-platform - ${maven-surefire.version} - - - - - - - - - - org.springframework.boot - spring-boot-dependencies - ${spring-boot.version} - pom - import - - - - - - - org.springframework.boot - spring-boot-starter-web - - - - - com.github.seregamorph - spring-test-smart-context - test - - - - com.github.seregamorph - demo-testkit - test - - - org.junit.platform - junit-platform-testkit - 1.10.0 - - - org.springframework.boot - spring-boot-starter-test - test - - - org.junit.jupiter - junit-jupiter - - - - - - org.junit.jupiter - junit-jupiter-api - 5.10.0 - - - org.junit.vintage - junit-vintage-engine - test - - - diff --git a/demo/demo-maven-junit-platform-junit4-boot24/src/test/java/com/github/seregamorph/testsmartcontext/MavenSmartDirtiesVintageEngineTest.java b/demo/demo-maven-junit-platform-junit4-boot24/src/test/java/com/github/seregamorph/testsmartcontext/MavenSmartDirtiesVintageEngineTest.java deleted file mode 100644 index 4d9aed4..0000000 --- a/demo/demo-maven-junit-platform-junit4-boot24/src/test/java/com/github/seregamorph/testsmartcontext/MavenSmartDirtiesVintageEngineTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.github.seregamorph.testsmartcontext; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage; -import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request; - -import com.github.seregamorph.testsmartcontext.demo.Integration1Test; -import com.github.seregamorph.testsmartcontext.demo.Integration2Test; -import com.github.seregamorph.testsmartcontext.demo.SampleIntegrationTest; -import com.github.seregamorph.testsmartcontext.testkit.TestEventTracker; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.platform.testkit.engine.EngineTestKit; - -public class MavenSmartDirtiesVintageEngineTest { - - private static final String ENGINE = "junit-vintage"; - - private static Map, SmartDirtiesTestsSupport.ClassOrderState>> prevEngineClassOrderStateMap; - - @BeforeClass - public static void beforeClass() { - prevEngineClassOrderStateMap = SmartDirtiesTestsSupport.setEngineClassOrderStateMap(null); - TestEventTracker.startTracking(); - } - - @AfterClass - public static void afterClass() { - TestEventTracker.stopTracking(); - SmartDirtiesTestsSupport.setEngineClassOrderStateMap(prevEngineClassOrderStateMap); - } - - @Test - public void testSuite() { - // to avoid confusion of duplicated test execution output - System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); - System.out.println(">>>EngineTestKit duplicating the suite>>>"); - - var events = EngineTestKit.execute(ENGINE, request() - .selectors(selectPackage("com.github.seregamorph.testsmartcontext.demo")) - .filters(new SmartDirtiesPostDiscoveryFilter()) - .build()) - .containerEvents(); - - // 5 = 3 ITs + 1 UTs + 1 suite - events.assertStatistics(stats -> stats - .started(5) - .succeeded(5) - .finished(5) - .aborted(0) - .failed(0)); - - assertEquals(List.of( - Integration1Test.class, - Integration2Test.class, - SampleIntegrationTest.class - ), new ArrayList<>(TestSmartDirtiesTestsHolder.getIntegrationTestClasses(ENGINE))); - - assertTrue(SmartDirtiesTestsSupport.isFirstClassPerConfig(Integration1Test.class)); - assertTrue(SmartDirtiesTestsSupport.isFirstClassPerConfig(Integration2Test.class)); - assertTrue(SmartDirtiesTestsSupport.isFirstClassPerConfig(SampleIntegrationTest.class)); - - assertTrue(SmartDirtiesTestsSupport.isLastClassPerConfig(Integration1Test.class)); - assertTrue(SmartDirtiesTestsSupport.isLastClassPerConfig(Integration2Test.class)); - assertTrue(SmartDirtiesTestsSupport.isLastClassPerConfig(SampleIntegrationTest.class)); - - TestEventTracker.assertConsumedEvent("Running Unit1Test"); - TestEventTracker.assertConsumedEvent("Creating context for com.github.seregamorph.testsmartcontext.demo.Integration1Test"); - TestEventTracker.assertConsumedEvent("Created context for com.github.seregamorph.testsmartcontext.demo.Integration1Test"); - TestEventTracker.assertConsumedEvent("Destroying context for com.github.seregamorph.testsmartcontext.demo.Integration1Test"); - TestEventTracker.assertConsumedEvent("Creating context for com.github.seregamorph.testsmartcontext.demo.Integration2Test"); - TestEventTracker.assertConsumedEvent("Created context for com.github.seregamorph.testsmartcontext.demo.Integration2Test"); - TestEventTracker.assertConsumedEvent("Destroying context for com.github.seregamorph.testsmartcontext.demo.Integration2Test"); - TestEventTracker.assertConsumedEvent("Creating context for com.github.seregamorph.testsmartcontext.demo.SampleIntegrationTest"); - TestEventTracker.assertConsumedEvent("Created context for com.github.seregamorph.testsmartcontext.demo.SampleIntegrationTest"); - TestEventTracker.assertConsumedEvent("Destroying context for com.github.seregamorph.testsmartcontext.demo.SampleIntegrationTest"); - TestEventTracker.assertEmpty(); - - System.out.println("<< - - - %d{HH:mm:ss} [%thread] %-5level %logger - %msg%n - - - - - - - diff --git a/demo/demo-maven-junit-platform-mixed-boot34/pom.xml b/demo/demo-maven-junit-platform-mixed-boot34/pom.xml index 40e6ee9..9d82ced 100644 --- a/demo/demo-maven-junit-platform-mixed-boot34/pom.xml +++ b/demo/demo-maven-junit-platform-mixed-boot34/pom.xml @@ -78,11 +78,6 @@ spring-boot-starter-test test - - org.junit.vintage - junit-vintage-engine - test - org.junit.support testng-engine diff --git a/demo/demo-maven-junit-platform-mixed-boot34/src/test/java/com/github/seregamorph/testsmartcontext/MavenSmartDirtiesVintageEngineTest.java b/demo/demo-maven-junit-platform-mixed-boot34/src/test/java/com/github/seregamorph/testsmartcontext/MavenSmartDirtiesVintageEngineTest.java deleted file mode 100644 index 6bc4b2e..0000000 --- a/demo/demo-maven-junit-platform-mixed-boot34/src/test/java/com/github/seregamorph/testsmartcontext/MavenSmartDirtiesVintageEngineTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.github.seregamorph.testsmartcontext; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage; -import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request; - -import com.github.seregamorph.testsmartcontext.demo.Integration2Test; -import com.github.seregamorph.testsmartcontext.demo.SampleDirtiesContextBeforeClassTest; -import com.github.seregamorph.testsmartcontext.testkit.TestEventTracker; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.platform.testkit.engine.EngineTestKit; - -public class MavenSmartDirtiesVintageEngineTest { - - private static final String ENGINE = "junit-vintage"; - - private static Map, SmartDirtiesTestsSupport.ClassOrderState>> prevEngineClassOrderStateMap; - - @BeforeAll - public static void beforeClass() { - prevEngineClassOrderStateMap = SmartDirtiesTestsSupport.setEngineClassOrderStateMap(null); - TestEventTracker.startTracking(); - } - - @AfterAll - public static void afterClass() { - TestEventTracker.stopTracking(); - SmartDirtiesTestsSupport.setEngineClassOrderStateMap(prevEngineClassOrderStateMap); - } - - @Test - public void testSuite() { - // to avoid confusion of duplicated test execution output - System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); - System.out.println(">>>EngineTestKit duplicating the suite>>>"); - - var events = EngineTestKit.execute(ENGINE, request() - .selectors(selectPackage("com.github.seregamorph.testsmartcontext.demo")) - .filters(new SmartDirtiesPostDiscoveryFilter()) - .build()) - .containerEvents(); - - // 4 = 2 ITs + 1 UTs + 1 suite - events.assertStatistics(stats -> stats - .started(4) - .succeeded(4) - .finished(4) - .aborted(0) - .failed(0)); - - assertEquals(List.of( - Integration2Test.class, - SampleDirtiesContextBeforeClassTest.class - ), new ArrayList<>(TestSmartDirtiesTestsHolder.getIntegrationTestClasses(ENGINE))); - - assertTrue(SmartDirtiesTestsSupport.isFirstClassPerConfig(Integration2Test.class)); - assertTrue(SmartDirtiesTestsSupport.isFirstClassPerConfig(SampleDirtiesContextBeforeClassTest.class)); - - assertTrue(SmartDirtiesTestsSupport.isLastClassPerConfig(Integration2Test.class)); - assertTrue(SmartDirtiesTestsSupport.isLastClassPerConfig(SampleDirtiesContextBeforeClassTest.class)); - - TestEventTracker.assertConsumedEvent("Running com.github.seregamorph.testsmartcontext.demo.Unit1Test.test"); - TestEventTracker.assertConsumedEvent("Creating context for com.github.seregamorph.testsmartcontext.demo.Integration2Test"); - TestEventTracker.assertConsumedEvent("Created context for com.github.seregamorph.testsmartcontext.demo.Integration2Test"); - TestEventTracker.assertConsumedEvent("Destroying context for com.github.seregamorph.testsmartcontext.demo.Integration2Test"); - TestEventTracker.assertConsumedEvent("Creating context for com.github.seregamorph.testsmartcontext.demo.SampleDirtiesContextBeforeClassTest"); - TestEventTracker.assertConsumedEvent("Created context for com.github.seregamorph.testsmartcontext.demo.SampleDirtiesContextBeforeClassTest"); - TestEventTracker.assertConsumedEvent("Running SampleDirtiesContextBeforeClassTest.test"); - TestEventTracker.assertConsumedEvent("Destroying context for com.github.seregamorph.testsmartcontext.demo.SampleDirtiesContextBeforeClassTest"); - TestEventTracker.assertEmpty(); - - System.out.println("<< - demo-maven-junit-platform-junit4-boot24 demo-maven-junit-platform-jupiter-boot35 demo-maven-junit-platform-jupiter-spring5 demo-maven-junit-platform-jupiter-spring7 diff --git a/spring-test-smart-context/pom.xml b/spring-test-smart-context/pom.xml index 014d231..246361e 100644 --- a/spring-test-smart-context/pom.xml +++ b/spring-test-smart-context/pom.xml @@ -21,6 +21,7 @@ 1.7.36 6.1.16 7.10.2 + 4.13.2 1.11.3 5.11.3 1.0.5 @@ -70,9 +71,9 @@ provided - org.junit.vintage - junit-vintage-engine - ${junit-jupiter.version} + junit + junit + ${junit4.version} provided diff --git a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/IntegrationTestFilter.java b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/IntegrationTestFilter.java index 4ff1ae3..e76b20a 100644 --- a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/IntegrationTestFilter.java +++ b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/IntegrationTestFilter.java @@ -6,12 +6,9 @@ import java.util.ServiceLoader; import java.util.Set; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.runner.RunWith; -import org.junit.runner.Runner; import org.springframework.context.ApplicationContextAware; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * Integration Test class filter that should be ordered (as they use spring context). The logic of this class can be @@ -56,10 +53,6 @@ protected boolean isIntegrationTest(Class testClass) { return true; } - if (JUnitPlatformSupport.isJunit4Present() && isIntegrationTestJUnit4(testClass)) { - return true; - } - //noinspection RedundantIfStatement if (JUnitPlatformSupport.isJunit5JupiterApiPresent() && isIntegrationTestJUnit5Jupiter(testClass)) { return true; @@ -68,20 +61,6 @@ protected boolean isIntegrationTest(Class testClass) { return false; } - /** - * This method should be only called if JUnit4 is on the classpath - */ - protected boolean isIntegrationTestJUnit4(Class testClass) { - // can be inherited, but cannot be meta-annotation - RunWith runWith = testClass.getAnnotation(RunWith.class); - if (runWith == null) { - return false; - } - Class runner = runWith.value(); - // includes org.springframework.test.context.junit4.SpringRunner - return SpringJUnit4ClassRunner.class.isAssignableFrom(runner); - } - /** * This method should be only called if JUnit5 Jupiter API is on the classpath */ diff --git a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/JUnitPlatformSupport.java b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/JUnitPlatformSupport.java index 7ef9196..f74ef4c 100644 --- a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/JUnitPlatformSupport.java +++ b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/JUnitPlatformSupport.java @@ -7,11 +7,6 @@ */ final class JUnitPlatformSupport { - @Deprecated - private static final boolean JUNIT_VINTAGE_ENGINE_PRESENT = isClassPresent( - "org.junit.vintage.engine.descriptor.RunnerTestDescriptor"); - - @Deprecated private static final boolean JUNIT4_PRESENT = isClassPresent( "org.junit.runner.RunWith"); @@ -21,11 +16,6 @@ final class JUnitPlatformSupport { private static final boolean JUNIT4_IDEA_TEST_RUNNER_PRESENT = isClassPresent( "com.intellij.junit4.JUnit4IdeaTestRunner"); - @Deprecated - static boolean isJUnitVintageEnginePresent() { - return JUNIT_VINTAGE_ENGINE_PRESENT; - } - @Deprecated static boolean isJunit4Present() { return JUNIT4_PRESENT; @@ -35,11 +25,6 @@ static boolean isJunit5JupiterApiPresent() { return JUNIT5_JUPITER_API_PRESENT; } - @Deprecated - static boolean isJUnit4IdeaTestRunnerPresent() { - return JUNIT4_IDEA_TEST_RUNNER_PRESENT; - } - private static boolean isClassPresent(String className) { return ClassUtils.isPresent(className, JUnitPlatformSupport.class.getClassLoader()); } diff --git a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesContextTestExecutionListener.java b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesContextTestExecutionListener.java index 4915751..bcdf8cc 100644 --- a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesContextTestExecutionListener.java +++ b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesContextTestExecutionListener.java @@ -12,7 +12,7 @@ * {@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}. Based on known list (ordered) * of tests to execute (reordered via {@link com.github.seregamorph.testsmartcontext.jupiter.SmartDirtiesClassOrderer} * for Jupiter classes, {@link com.github.seregamorph.testsmartcontext.testng.SmartDirtiesSuiteListener} for TestNG - * classes or {@link SmartDirtiesPostDiscoveryFilter} for JUnit 4 classes), the last test in each group that shares the + * classes, the last test in each group that shares the * same configuration (=share the same spring context) will automatically close the ApplicationContext on after-class, * which will release resources as well (like Docker containers defined as spring beans). See detailed explanation README. diff --git a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesPostDiscoveryFilter.java b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesPostDiscoveryFilter.java deleted file mode 100644 index 2f62aa0..0000000 --- a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesPostDiscoveryFilter.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.github.seregamorph.testsmartcontext; - -import static java.util.Collections.singletonList; - -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; -import org.junit.platform.engine.FilterResult; -import org.junit.platform.engine.TestDescriptor; -import org.junit.platform.launcher.PostDiscoveryFilter; -import org.junit.vintage.engine.descriptor.RunnerTestDescriptor; -import org.junit.vintage.engine.descriptor.VintageTestDescriptor; -import org.springframework.lang.NonNull; -import org.springframework.lang.Nullable; - -/** - * Auto-discovered JUnit platform {@link PostDiscoveryFilter} which reorders and groups integration test classes - * according to their configuration. Note: this class sorts only JUnit 4 tests executed via - * vintage-engine. - *

- * For TestNG test classes - see {@link com.github.seregamorph.testsmartcontext.testng.SmartDirtiesSuiteListener}, for - * Jupiter test classes - see {@link com.github.seregamorph.testsmartcontext.jupiter.SmartDirtiesClassOrderer}. - * - * @author Sergey Chernov - * @deprecated support of JUnit 4 will be removed in 1.0 release - */ -@Deprecated -public class SmartDirtiesPostDiscoveryFilter implements PostDiscoveryFilter { - - private static final String ENGINE = "junit-vintage"; - - @Override - public FilterResult apply(TestDescriptor testDescriptor) { - List childrenToReorder = testDescriptor.getChildren().stream() - .filter(childTestDescriptor -> { - // If it is a testng-engine running TestNG test, rely on SmartDirtiesSuiteListener, because - // TestNG will alphabetically reorder it first anyway. - // Jupiter engine has its own sorting via SmartDirtiesClassOrderer, so skip them as well. - // Reorder only JUnit4 here: - return getTestClassOrNull(childTestDescriptor) != null; - }) - .collect(Collectors.toList()); - - if (childrenToReorder.isEmpty()) { - return FilterResult.included("Empty list"); - } - - Set> uniqueClasses = childrenToReorder.stream() - .map(SmartDirtiesPostDiscoveryFilter::getTestClass) - .collect(Collectors.toSet()); - if (uniqueClasses.size() == 1) { - // This filter is executed several times during discover and execute phases and - // it's not possible to distinguish them here. Sometimes per single test is sent as argument, - // sometimes - the whole suite. If it's a suite more than 1, we can save it and never update. - // If it's 1 - we should also distinguish single test execution. - if (SmartDirtiesTestsSupport.classOrderStateMapSize(ENGINE) <= 1) { - Class testClass = getTestClass(childrenToReorder.get(0)); - SmartDirtiesTestsSupport.setTestClassesLists(ENGINE, singletonList(singletonList(testClass))); - } - - // the logic here may differ for JUnit 4 via Maven vs IntelliJ: - // Maven calls this filter several times (first per each test, then with all tests) - return FilterResult.included("Skipping single element"); - } - - childrenToReorder.forEach(testDescriptor::removeChild); - - SmartDirtiesTestsSorter sorter = SmartDirtiesTestsSorter.getInstance(); - List>> testClassesLists; - try { - testClassesLists = sorter.sort(childrenToReorder, SmartDirtiesPostDiscoveryFilter::getTestClass); - } catch (Throwable e) { - SmartDirtiesTestsSupport.setFailureCause(e); - throw e; - } - - childrenToReorder.forEach(testDescriptor::addChild); - - SmartDirtiesTestsSupport.setTestClassesLists(ENGINE, testClassesLists); - - return FilterResult.included("sorted"); - } - - @NonNull - private static Class getTestClass(TestDescriptor testDescriptor) { - Class testClass = getTestClassOrNull(testDescriptor); - if (testClass == null) { - throw new UnsupportedOperationException("Unsupported TestDescriptor type " + testDescriptor.getClass() - + ", failed to obtain test class"); - } - return testClass; - } - - @SuppressWarnings("RedundantIfStatement") - @Nullable - private static Class getTestClassOrNull(TestDescriptor testDescriptor) { - if (JUnitPlatformSupport.isJUnitVintageEnginePresent()) { - Class testClass = getTestClassJUnitVintageEngine(testDescriptor); - if (testClass != null) { - return testClass; - } - } - - return null; - } - - @Nullable - private static Class getTestClassJUnitVintageEngine(TestDescriptor testDescriptor) { - if (testDescriptor instanceof RunnerTestDescriptor) { - RunnerTestDescriptor runnerTestDescriptor = (RunnerTestDescriptor) testDescriptor; - return runnerTestDescriptor.getDescription().getTestClass(); - } - if (testDescriptor instanceof VintageTestDescriptor) { - VintageTestDescriptor vintageTestDescriptor = (VintageTestDescriptor) testDescriptor; - return vintageTestDescriptor.getDescription().getTestClass(); - } - return null; - } -} diff --git a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesTestsSupport.java b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesTestsSupport.java index 262473e..40c9123 100644 --- a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesTestsSupport.java +++ b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/SmartDirtiesTestsSupport.java @@ -15,9 +15,12 @@ import java.util.Set; import java.util.stream.Collectors; import org.junit.jupiter.api.ClassOrderer; +import org.junit.runner.RunWith; +import org.junit.runner.Runner; import org.springframework.lang.Nullable; import org.springframework.test.context.BootstrapUtilsHelper; import org.springframework.test.context.MergedContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * This class should only be used internally by the framework. @@ -117,18 +120,9 @@ private static ClassOrderState getOrderState(Class testClass) { throw new UncheckedIOException(e); } } - if (JUnitPlatformSupport.isJunit4Present() && JUnitPlatformSupport.isJUnit4IdeaTestRunnerPresent()) { - System.err.println("The test is started via IDEA old JUnit 4 runner (not vintage), " + - "the Smart DirtiesContext behaviour is disabled."); - if (!JUnitPlatformSupport.isJunit5JupiterApiPresent()) { - //@formatter:off - System.err.println("If you add org.junit.jupiter:junit-jupiter-api test dependency, \n" + - "it will allow to run packages/modules with tests with Smart DirtiesContext semantics via " + - "IDEA. See \n" + - "https://youtrack.jetbrains.com/issue/IDEA-343605/junit-vintage-engine-is-not-preferred-by-default\n" + - "for details."); - //@formatter:on - } + if (JUnitPlatformSupport.isJunit4Present() && isIntegrationTestJUnit4(testClass)) { + System.err.println(testClass.getName() + " is a JUnit 4 test class, " + + "the Smart DirtiesContext behaviour is not supported. The last version supporting JUnit 4 is 0.9"); return null; } throw new IllegalStateException("engineClassOrderStateMap is not initialized", failureCause); @@ -211,4 +205,15 @@ static Map, ClassOrderState>> setEngineClassOrderStateMap( SmartDirtiesTestsSupport.engineClassOrderStateMap = engineClassOrderStateMap; return prev; } + + private static boolean isIntegrationTestJUnit4(Class testClass) { + // can be inherited, but cannot be meta-annotation + RunWith runWith = testClass.getAnnotation(RunWith.class); + if (runWith == null) { + return false; + } + Class runner = runWith.value(); + // includes org.springframework.test.context.junit4.SpringRunner + return SpringJUnit4ClassRunner.class.isAssignableFrom(runner); + } } diff --git a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/junit4/AbstractJUnit4SpringIntegrationTest.java b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/junit4/AbstractJUnit4SpringIntegrationTest.java deleted file mode 100644 index 0bb9b46..0000000 --- a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/junit4/AbstractJUnit4SpringIntegrationTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.github.seregamorph.testsmartcontext.junit4; - -import com.github.seregamorph.testsmartcontext.SmartDirtiesContextTestExecutionListener; -import org.springframework.test.context.TestExecutionListeners; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; - -/** - * Base class for JUnit 4 integration tests that create spring context. Supports - * {@link SmartDirtiesContextTestExecutionListener} semantics to optimize IT suite execution. - * - * @author Sergey Chernov - * @see SmartDirtiesContextTestExecutionListener - * @deprecated support of JUnit 4 will be removed in 1.0 release - */ -@Deprecated -@TestExecutionListeners(listeners = { - SmartDirtiesContextTestExecutionListener.class, -}, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS) -public abstract class AbstractJUnit4SpringIntegrationTest extends AbstractJUnit4SpringContextTests { - -} diff --git a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/jupiter/SmartDirtiesClassOrderer.java b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/jupiter/SmartDirtiesClassOrderer.java index 816df94..97eeab5 100644 --- a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/jupiter/SmartDirtiesClassOrderer.java +++ b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/jupiter/SmartDirtiesClassOrderer.java @@ -18,8 +18,7 @@ * configuration. Also stores information about last integration class per configuration, which is used by * {@link com.github.seregamorph.testsmartcontext.SmartDirtiesContextTestExecutionListener}. *

- * For TestNG test classes - see {@link com.github.seregamorph.testsmartcontext.testng.SmartDirtiesSuiteListener}, for - * JUnit 4 test classes - see {@link com.github.seregamorph.testsmartcontext.SmartDirtiesPostDiscoveryFilter}. + * For TestNG test classes - see {@link com.github.seregamorph.testsmartcontext.testng.SmartDirtiesSuiteListener}. * * @author Sergey Chernov */ diff --git a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/testng/SmartDirtiesSuiteListener.java b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/testng/SmartDirtiesSuiteListener.java index 6f6e1b5..f6654b4 100644 --- a/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/testng/SmartDirtiesSuiteListener.java +++ b/spring-test-smart-context/src/main/java/com/github/seregamorph/testsmartcontext/testng/SmartDirtiesSuiteListener.java @@ -17,8 +17,7 @@ * Reorders TestNG test classes in suite grouping ITs with the same context configuration to minimize number of parallel * existing contexts. *

- * For Jupiter test classes - see {@link com.github.seregamorph.testsmartcontext.jupiter.SmartDirtiesClassOrderer}, for - * JUnit 4 test classes - see {@link com.github.seregamorph.testsmartcontext.SmartDirtiesPostDiscoveryFilter}. + * For Jupiter test classes - see {@link com.github.seregamorph.testsmartcontext.jupiter.SmartDirtiesClassOrderer}. * * @author Sergey Chernov */ diff --git a/spring-test-smart-context/src/main/resources/META-INF/services/org.junit.platform.launcher.PostDiscoveryFilter b/spring-test-smart-context/src/main/resources/META-INF/services/org.junit.platform.launcher.PostDiscoveryFilter deleted file mode 100644 index 5b913ee..0000000 --- a/spring-test-smart-context/src/main/resources/META-INF/services/org.junit.platform.launcher.PostDiscoveryFilter +++ /dev/null @@ -1 +0,0 @@ -com.github.seregamorph.testsmartcontext.SmartDirtiesPostDiscoveryFilter