STS 3.9.2(Eclipse Oxygen 4.7.2)JDK8项目JUnit5测试启动报错求助
Hey, I’ve dealt with this exact issue when running JUnit 5 tests in JDK 8 with Eclipse—let’s get this sorted out. The root cause here is that your project is missing the JUnit Platform Launcher dependency, which the Eclipse JDT runner needs to bootstrap JUnit 5 tests. The solutions you found for JDK 9 don’t apply here, so let’s focus on JDK 8-compatible fixes:
1. Fix Your Build Dependencies (Most Critical)
First, make sure you have all three core JUnit 5 modules included in your Maven or Gradle setup, using versions that fully support JDK 8 (JUnit 5.7+ is safe here, and keep versions aligned across modules):
Maven (pom.xml)
Add these dependencies to your <dependencies> block:
<!-- JUnit 5 API for writing tests --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.9.2</version> <scope>test</scope> </dependency> <!-- JUnit 5 test execution engine --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.9.2</version> <scope>test</scope> </dependency> <!-- JUnit Platform Launcher (fixes the missing LauncherFactory class) --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.9.2</version> <scope>test</scope> </dependency>
Run mvn clean install to refresh dependencies and ensure everything is downloaded.
Gradle (build.gradle)
For Groovy DSL:
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.2' testImplementation 'org.junit.platform:junit-platform-launcher:1.9.2'
For Kotlin DSL:
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.2") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.2") testImplementation("org.junit.platform:junit-platform-launcher:1.9.2")
Run gradle clean build --refresh-dependencies to sync and rebuild.
2. Verify IDE Run Configuration (Eclipse-Specific)
Since your error comes from Eclipse’s JDT runner, double-check your test run config:
- Right-click your test class → Run As → Run Configurations
- Select the JUnit 5 configuration for your test
- Go to the Classpath tab
- Make sure
junit-platform-launcher-*.jar,junit-jupiter-api-*.jar, andjunit-jupiter-engine-*.jarare listed under User Entries - If any are missing, click Add Library → select JUnit → choose JUnit 5 and follow the prompts to add the required jars.
3. Update Eclipse’s JUnit Plugin
Older versions of Eclipse’s JUnit 5 plugin can have compatibility issues with JDK 8:
- Go to Help → Check for Updates
- Look for updates related to JUnit (e.g., "Eclipse JUnit 5 Support")
- Install any available updates, restart Eclipse, and try running tests again.
4. Clean IDE Caches and Project State
Sometimes cached files or corrupted build state cause dependency issues:
- In Eclipse, go to Project → Clean → select your project and click Clean
- Optionally, delete the
.classpathand.projectfiles from your project root, then right-click the project → Configure → Convert to Maven/Gradle Project to re-sync dependencies - If you’re still having issues, delete the JUnit folders from your local Maven/Gradle repo (e.g.,
~/.m2/repository/org/junit) and re-download dependencies.
Quick Test to Validate
Create a simple test class to confirm the fix works:
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class Jdk8JUnit5Test { @Test public void basicTest() { assertTrue(true); } }
Right-click this class and run it as a JUnit 5 test—if it passes, you’re good to go!
内容的提问来源于stack exchange,提问作者codependent

