如何使用 junit 5 为 Eclipse 编写测试套件?

问题描述

我正在尝试通过测试套件中的 junit5 测试运行。但是我收到一个错误

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;

import com.test.studentservice.controllers.StudentControllerTest2;
import com.test.studentservice.repo.StudentServiceRepoDataTest;
import com.test.studentservice.service.StudentServiceTest;

@RunWith(JUnitPlatform.class)
@SelectClasses({StudentControllerTest2.class,StudentServiceRepoDataTest.class,StudentServiceTest.class})
public class StudentServiceTestSuite {

}

这是我得到的错误

this is the error im getting

谢谢

解决方法

您收到此错误,因为测试选项卡中的JUnit启动配置运行>运行配置...)中,您选择测试运行器选项JUnit 5而不是JUnit 4 >

versions = ['microsoft windows 2016','win2016','win2012','windows 2012','windows server 2012','windows server 2008 r2','windows server 2008','windows 2019','windows 2012 r2 std','win2003','windows server 2003','windows 2003'] if test_input.strip().lower() in versions: ... 用于将 JUnit 5 测试作为 JUnit 4 测试运行,以防工具仅支持 JUnit 4 但不支持 JUnit 5 ( for details see this answer)。但是您不需要它,因为 Eclipse 支持 JUnit 5 并且不应该使用它,因为通过 JUnit 4 API 运行它有一些限制。

Baeldung - A Guide to JUnit 5 - 7. Test Suites 其中 you mentioned in a comment you are following here 是错误的,或者至少在这一点上具有误导性。代替 JUnit 测试套件类,标记您的测试并使用它们来包含或排除测试(在 JUnit 启动配置中,在 Test 选项卡中,点击 Configure... em> 按钮)。