要在Spring5项目中引入JUnit5,需要以下步骤:

  1. 添加JUnit5依赖:
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.7.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.7.0</version>
    <scope>test</scope>
</dependency>
  1. 在测试类中使用JUnit5注解,例如:
import org.junit.jupiter.api.Test;

public class MyTest {
    @Test
    void myTest() {
        // test code here
    }
}
  1. 运行测试。可以使用Maven命令mvn test或在IDE中运行测试。

注意,如果使用的是JUnit4的注解(例如@Test),需要在测试类上添加@ExtendWith(JUnitPlatform.class)注解才能使用JUnit5。例如:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@ExtendWith(SpringExtension.class)
public class MyTest {
    @Test
    void myTest() {
        // test code here
    }
}
``
spring5项目 引入junit5

原文地址: https://www.cveoy.top/t/topic/fuy9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录