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({
"13536764015, 123456",
"18475465437, 123456",
"18475465438, 123456"
})
public void loginTest(String mobile,String password){
// String mobile="13536764015";
// String password="123456";
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);
}
}`