在 Spring Boot 中使用参数化测试可以使用 JUnit5 的'@ParameterizedTest'注解。

以下是步骤:

  1. 导入 JUnit5 的依赖:
<dependency>
   <groupId>org.junit.jupiter</groupId>
   <artifactId>junit-jupiter-api</artifactId>
   <version>5.5.2</version>
   <scope>test</scope>
</dependency>
  1. 创建测试类,并使用'@ParameterizedTest'注解标记测试方法
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class MyTests {
    @ParameterizedTest
    @ValueSource(ints = {1, 2, 3})
    void testWithInts(int argument) {
        Assertions.assertTrue(argument > 0 && argument < 4);
    }
}
  1. 在'@ParameterizedTest'注解中使用'@ValueSource'注解指定测试参数
@ValueSource(ints = {1, 2, 3})

这样就可以使用参数化测试来测试多个参数。

Spring Boot 参数化测试指南:使用 JUnit5 进行高效测试

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

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