0

I have list strings with some urls:

  • localhost:80/my/first/url,
  • localhost:80/my/second/url

there can be 2, 5 or more urls. I must check which adres respond me with error 500.

@Test()  //bad test
public void checkErrorStatusForURLs() {
    for(String url : urlList()) {
        given().when().get(url)
               .then().statusCode(CoreMatchers.not(500));
    }
}

I dont want write tests for each url. Can i do it on one test? How to do it correctly?

tgogos
  • 23,218
  • 20
  • 96
  • 128
Tom
  • 1
  • 4
  • Possible duplicate of [Java unit test for different input data](https://stackoverflow.com/questions/10028188/java-unit-test-for-different-input-data) – Arnaud Jul 13 '17 at 14:13

2 Answers2

0

You can extract statusCode and then check what evere condition you want like this:

for(String url : urlList()) {
    int statusCode = given().when().get(url).then().extract().statusCode();
    if(500 == statusCode) {
        //add it to a list??
    }
}
ddarellis
  • 3,912
  • 3
  • 25
  • 53
0

Take a look at Junit 4's parameterized tests as described here: https://github.com/junit-team/junit4/wiki/parameterized-tests

Something along the lines of this may work for you:

@RunWith(Parameterized.class)
public class FibonacciTest {

@Parameters
public static Iterable<? extends Object> urls() {
    return Arrays.asList("localhost:80/my/first/url", "localhost:80/my/second/url" );
}

@Parameter 
public /* NOT private */ String url;

@Test
public void checkErrorStatusForURLs() {
        given().when().get(url)
               .then().statusCode(CoreMatchers.not(500));
 ...
}
}
Michael Peacock
  • 2,011
  • 1
  • 11
  • 14