JUnit STS报错:未找到JUnit 5测试运行器及方法缺失问题求助
Spring Batch JUnit 5测试报错:
NoSuchMethodError on Launcher.execute() 我从Spring Batch多步骤Java配置示例中复制了业务程序,参照官方测试文档编写了JUnit 5测试用例,但在Eclipse中运行测试时始终遇到NoSuchMethodError错误。尝试调整JUnit依赖版本、修改项目库配置后仍未解决,具体信息如下:
测试代码
package com.example.demo; import org.springframework.batch.core.JobExecution; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.batch.test.context.SpringBatchTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.junit.Assert; import org.junit.jupiter.api.*; import org.junit.runner.RunWith; @SpringBatchTest @RunWith(SpringRunner.class) @ContextConfiguration(classes=DemoSpringBatch1Application.class) class DemoSpringBatch1ApplicationTests { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @Test public void testJob() throws Exception { JobExecution jobExecution = jobLauncherTestUtils.launchJob(); Assert.assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode()); } }
Maven POM配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo_spring_batch_1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo_spring_batch_1</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <junit-jupiter.version>5.2.0</junit-jupiter.version> <junit-platform.version>1.2.0</junit-platform.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-test</artifactId> <scope>test</scope> </dependency> <!-- <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-platform-engine</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>${junit-platform.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>repository.spring.release</id> <name>Spring GA Repository</name> <url>http://repo.spring.io/release</url> </repository> </repositories> </project>
错误栈信息
java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/TestPlan;[Lorg/junit/platform/launcher/TestExecutionListener;)V at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
问题分析与解决方案
这个错误的核心是JUnit平台组件版本不兼容:Eclipse内置的JUnit 5 runner依赖的Launcher.execute方法签名,和你手动指定的junit-platform-launcher版本(1.2.0)不匹配。同时测试类存在JUnit 4和JUnit 5注解混用的问题,进一步加剧了兼容性冲突。
1. 统一JUnit依赖版本,移除手动指定的组件
Spring Boot的spring-boot-starter-test已经内置了完全兼容的JUnit 5依赖包,不需要手动指定junit-jupiter.version和junit-platform.version。删除POM中这些自定义版本属性,以及单独引入的junit-platform-launcher依赖,让Spring Boot父POM自动管理版本兼容性:
修改后的POM核心依赖部分:
<properties> <java.version>1.8</java.version> <!-- 移除自定义的JUnit版本属性 --> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> </dependencies>
2. 修正测试类的注解混用问题
你的测试类同时使用了JUnit 4的@RunWith(SpringRunner.class)和JUnit 5的注解,这会导致运行时冲突。@SpringBatchTest已经内置了Spring扩展支持,直接移除@RunWith注解即可,同时替换JUnit 4的断言类为JUnit 5版本:
修改后的测试类:
package com.example.demo; import org.springframework.batch.core.JobExecution; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.batch.test.context.SpringBatchTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @SpringBatchTest @ContextConfiguration(classes=DemoSpringBatch1Application.class) class DemoSpringBatch1ApplicationTests { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @Test void testJob() throws Exception { JobExecution jobExecution = jobLauncherTestUtils.launchJob(); assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode()); } }
3. 清理Eclipse项目配置
- 右键项目 →
Maven > Update Project,勾选Force Update of Snapshots/Releases,确保依赖完全同步。 - 检查项目的
Java Build Path,移除所有手动添加的JUnit库,只保留Maven管理的依赖。 - 执行
Project > Clean清理项目,然后重启Eclipse确保缓存生效。
内容的提问来源于stack exchange,提问作者Aavik
相关产品推荐
相关产品推荐

