放心。随机"无法在null对象上调用方法getAt()"在Maven中运行多个测试套件时出错

时间:2017-03-28 20:01:56

标签: java maven random junit4 rest-assured

我有几个使用Rest-Assured,JUnitParams和JUnit4的测试套件,这个套件共享一个包含这部分代码的@Test方法,代码从JSON响应中随机获取一个项目,其中包含&#34 ; children_uuids"它们的价值:

            // 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

我在某些套件中随机出现以下错误:

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);
            }

0 个答案:

没有答案