在springboot中如何使用参数化测试
在Spring Boot中使用参数化测试可以使用JUnit5的@ParameterizedTest注解。
以下是步骤:
- 导入JUnit5的依赖:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
- 创建测试类,并使用@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);
}
}
- 在@ParameterizedTest注解中使用@ValueSource注解指定测试参数
@ValueSource(ints = {1, 2, 3})
这样就可以使用参数化测试来测试多个参数
原文地址: https://www.cveoy.top/t/topic/fH2o 著作权归作者所有。请勿转载和采集!