Eclipse中JUnit 5(Jupiter)突然出现<terminated>错误求助
问题描述
运行首个JUnit测试时,JUnit Runner窗口未弹出显示测试结果,控制台仅显示<terminated>提示。此前相同配置的3个项目测试均正常,近期突然全部出现该问题。已排查Maven配置、更换工作区、重装JDK和Eclipse,但问题仍未解决。
相关代码
Appointment.java
/************************* * Name: Summer Bernotas * Course: CS-320 Software Testing, Automation, and Quality Assurance * Date: June 5th, 2024 *************************/ package Appointment; import java.util.concurrent.atomic.AtomicInteger; import java.util.Date; public class Appointment { private final String aptID; private final static AtomicInteger initializeAptID = new AtomicInteger(); private final static byte DESCRIPTION_LENGTH = 50; private String aptDescription = "DEFAULT"; //////////////// // initializes and creates the Appointment constructor setting exceptions the description length. // The appointment ID is also initialized and verified to only each having 10 characters. //////////////// public Appointment(String aptDescription) { this.aptID = String.valueOf(initializeAptID.getAndIncrement() % 10); if (aptDescription == null || aptDescription.length() > DESCRIPTION_LENGTH) { throw new IllegalArgumentException("Description can not be left blank and can only be " + DESCRIPTION_LENGTH + " characters long"); } else { this.aptDescription = aptDescription; } } //////////////// // initializes getters for Appointment class //////////////// public String getAptID() { return this.aptID; } public String getAptDescription() { return this.aptDescription; } //////////////// // initializes setters for Appointment class //////////////// public void setAptDescription(String aptDescription) { if (aptDescription == null || aptDescription.length() > DESCRIPTION_LENGTH) { throw new IllegalArgumentException("Description can not be left blank and can only be " + DESCRIPTION_LENGTH + " characters long"); } this.aptDescription = aptDescription; } }
AppointmentTest.java
package Test; import org.junit.Test; import org.junit.jupiter.api.Assertions; import Appointment.Appointment; public class AppointmentTest { @Test public void aptDescriptionLengthTEST() { Assertions.assertThrows(IllegalArgumentException.class, () -> { new Appointment("Description that is too long for the required argument length amount"); }); } }
排查与解决建议
修复JUnit版本混用问题
测试类同时引入了JUnit 4的org.junit.Test注解和JUnit 5的org.junit.jupiter.api.Assertions类,跨版本混用会导致测试执行逻辑冲突,无法正常触发JUnit Runner。解决:将import org.junit.Test替换为JUnit 5的注解import org.junit.jupiter.api.Test。统一Maven中JUnit依赖
确保pom.xml中使用统一的JUnit 5依赖,避免版本冲突:<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.9.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.9.2</version> <scope>test</scope> </dependency>检查Eclipse测试运行配置
- 右键测试类 → Run As → Run Configurations
- 在JUnit配置项中,确认Test runner选择
JUnit Jupiter(对应JUnit 5),而非JUnit 4 - 若配置异常,删除现有配置后重新创建测试运行配置
验证测试类目录结构
确保测试类放置在Maven标准测试目录src/test/java下,Eclipse会自动识别该目录下的测试类,若路径错误可能导致JUnit无法扫描到测试用例。
内容的提问来源于stack exchange,提问作者Summer
相关产品推荐
相关产品推荐

