I am using the rest-assured library to test our REST api that deals with data on sports. In short I have 2 different @Test methods to call per sport, one @Test method to make multiple GET requests to gather all athlete image urls and store in a static ArrayList, and the other method to instantiate a SoftAssert object and actually call all of the url's in a for loop and soft assert a 200 response code. I then do a assertAll() at the end of the 2nd test method.
For example - I have a @Test getSoccerAthletes() which gathers all the urls from the response, the method will repeat until all athlete urls are gathered as the response is limited to 250 athletes at a time. After this method finishes, then the 2nd @Test method for Soccer will execute, it is named testSoccerAthletes() and you can see that it uses dependsOnMethods. Below is the setup.
@Test(priority = 1)
public static void getSoccerAthletes() {
baseURI = "https://XXXXX";
basePath = "XXXXX";
Response res = given().queryParam("apikey", "XXXXXXXX")
.queryParam("top", "250")
.queryParam("skip", soccerSkip)
.when().get();
Assert.assertEquals(res.statusCode(), 200);
JsonPath jPath = res.jsonPath();
System.out.println("Soccer: currentPageStart/totalCount " + jPath.getInt("currentPageStart")
+ "/" + jPath.getInt("totalCount"));
if (soccerSkip == 0) {
allSoccerAthletes = jPath.get("page.links.headshots.full");
} else {
ArrayList<String> athletes = jPath.get("page.links.headshots.full");
allSoccerAthletes.addAll(athletes);
}
if (jPath.getInt("currentPageStart") + 250 < jPath.getInt("totalCount")) {
soccerSkip += 250;
getSoccerAthletes();
}
}
@Test(priority = 2, dependsOnMethods = {"getSoccerAthletes"})
public static void testSoccerAthletes() {
SoftAssert softAssert = new SoftAssert();
for (int i = 0; i < allSoccerAthletes.size(); i++) {
String url = allSoccerAthletes.get(i);
System.out.println("Soccer Athlete: " + i + "/" + allSoccerAthletes.size());
Response res = when().head(url);
softAssert.assertEquals(res.statusCode(), 200, "Failed url: " + url);
if (i == allSoccerAthletes.size() - 1)
allSoccerAthletes.clear();
}
softAssert.assertAll();
}
I am seeing varying results, some of which have failures from the @Test testXXXXAthletes methods mixed up. This first suggestion online was to instantiate a SoftAssert object in each @Test method (which I currently am for every testXXXXAthletes method) so that can't be it.
I am starting to lean toward there are thread safety issues, but I am not really sure how to move forward with a solution. Reasons I believe there are thread safety issues are from some articles I have looked into, but do not fully understand. Articles --> https://learn2automate.wordpress.com/2017/07/13/parallel-testng-soft-assertions-the-right-way/, Retrieve test name on TestNG, https://github.com/rest-assured/rest-assured/issues/1420
Any help in resolving my issues would be greatly appreciated! Btw I have also looked into using the @DataProvider annotations and that brings me to other questions about the structure of these tests. The getXXXAthletes methods are acting as data providers to the testXXXAthletes methods, but they have the dependsOnMethods attribute. Should I be using both DataProvider and dependsOnMethods, or one over the other?