junit5使用参数化注解时报错
junit5使用参数化注解@ParameterizedTest、@CsvSource注解时遇到的问题:
1.无法使用@ParameterizedTest、@CsvSource注解,原因是pom.xml文件中缺少junit5的依赖
解决方法:pom.xml文件中加入以下依赖:
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency>
2.使用@ParameterizedTest、@CsvSource注解后报如下错误:
原因:也是junit依赖包的问题
解决方法,导入以下junit相关依赖包:
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.6.2</version> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-runner</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.6.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency>
3.使用参数化后断言问题,使用assertTrue断言
注意导包:import static org.junit.Assert.*;
示例:
package Test; import Interfaces.LoginTest; import io.restassured.response.Response; import static org.junit.Assert.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; public class Login { @ParameterizedTest @CsvSource({ "13536764011, 123456", "18271235437, 123456", "18175124438, 123456" }) public void loginTest(String mobile,String password){ Response loginResponse = LoginTest.loginByiMobile(mobile,password); Integer stateCode = loginResponse.path("stateCode"); String access_token=loginResponse.path("data.access_token"); assertTrue (stateCode.equals(200)); System.out.println(access_token); } }