Java 请放心。“随机”;无法对null对象调用方法getAt();在Maven中运行多个测试套件时出错
我有几个测试套件使用Rest Assured、JUnitParams和JUnit4,这些套件共享一个@test方法,其中包含这部分代码,代码从JSON响应中随机获取一个项目,其中包含一个“children_uuids”值:Java 请放心。“随机”;无法对null对象调用方法getAt();在Maven中运行多个测试套件时出错,java,maven,random,junit4,rest-assured,Java,Maven,Random,Junit4,Rest Assured,我有几个测试套件使用Rest Assured、JUnitParams和JUnit4,这些套件共享一个@test方法,其中包含这部分代码,代码从JSON响应中随机获取一个项目,其中包含一个“children_uuids”值: // Test case response = serviceCaller.call(); Boolean success = response.then().extract().path("Succe
// Test case
response = serviceCaller.call();
Boolean success = response.then().extract().path("Success");
assertTrue(success);
// get random content data
Integer contentAmmount = response.body().path("Contents.Contents.size()");
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(contentAmmount);
while (response.body().path(
String.join("", "Contents.Contents[", Integer.toString(randomInt), "].children_uuids"))
.toString().isEmpty()) {
randomInt = randomGenerator.nextInt(contentAmmount);
}
contentTitle = response.then().extract()
.path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].title"));
contentUuid = response.then().extract()
.path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].uuid"));
contentChildrenUuid = response.body().path(
String.join("", "Contents.Contents[", Integer.toString(randomInt), "].children_uuids.item[0]"));
System.out.println(contentTitle + "#" + contentChildrenUuid);
问题是,如果我只使用以下命令运行一个测试套件:
mvn test -Dtest=TestSuite01#testCase01
mvn test -Dtest=TestSuite*#testCase01
测试运行正常,但如果我使用命令运行所有套件:
mvn test -Dtest=TestSuite01#testCase01
mvn test -Dtest=TestSuite*#testCase01
我在某些套件中随机出现以下错误:
java.lang.IllegalArgumentException: Cannot invoke method getAt() on null object
Results :
Tests in error:
TestSuite01>ApiServiceCommon.testCase01:1023 » IllegalArgument
这是我得到的JSON响应的一个片段:
Response:
{
"Contents": {
"Title": "Disponible",
"Count": "31",
"Page": "1",
],
"Contents": [
{
"uuid": "asdn359614nf3f",
"categories": "Películas",
"sub_categories": "",
"audio_tracks": "en,es",
"operators": "All",
"children_ids": {},
"children_uuids": {},
"parent_ids": {},
"parent_uuids": {},
"tags": {
"item": [
"Work"
]
}
},
{
"uuid": "kgirnxfnwoer35",
"categories": "Películas",
"sub_categories": "",
"audio_tracks": "en,es",
"operators": "All",
"children_ids": {},
"children_uuids": {
"item": [
"091e2e53-cfc9-44f0-b691-83cf57146912_ShutterIslandTrailerVO"
]
},
"parent_ids": {},
"parent_uuids": {},
"tags": {
"item": [
"Work"
]
}
}
]
}
}
提前谢谢
更新:我发现问题在于它返回空“children_uuids”字段的次数,我尝试了以下方法但没有成功:
while (response.body()
.path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].children_uuids.item"))
.toString().equals(null)) {
System.out.println(randomInt);
randomInt = randomGenerator.nextInt(contentAmmount);
}