I have 2 endpoints that do the same: receive the same parameters, and respond with identical responses, the only difference is a baseUrl. I have about 50+ parameterized tests under ApiTests class
public class ApiTests {
@ParameterizedTest
@EmptySource
@ValueSource(strings = {"", " ", "abc", "ABC", "true", "adsfkjsdgfhgsjdgfjgshdjgfgsd"})
@Description("Get valid customer info")
public void testGetData(String customer) {
Response getCustomerData = getCustomerDataResponse(customer);
//Do validation
}
@ParameterizedTest
@ValueSource(ints = {Integer.MIN_VALUE, 0, 100, Integer.MAX_VALUE})
@Description("Get invalid customer info")
public void testGetData2(int customer) {
Response getCustomerData = getCustomerDataResponse(customer);
//Do validation;
}
//more tests
public Response getCustomerDataResponse(Object customer) {
Response getCustomerData = given().
header("Authorization", "Bearer " + token).
pathParam("customer", customer).
contentType(ContentType.JSON).
when().
get("abc.com/v1/customers/{customer}");
return getCustomerData;
}
}
I want to run the entire class of tests once with "abc.com/v1/customers/{customer}" and after that with "abcdef.com/v1/customers/{customer}" It should be a part of a nightly run for a long period of time. Any idea how to do it without copy pasting the entire class just because it should run with a different baseUrl?
Because I already have parametrized tests I can't put the base url as a parameter for my test methods.
One solution I've used myself a few times is to create an abstract class, put all the test methods in there, and sub class it. You can even nest them in your existing class:
class ApiTests {
@Nested
class AbcApiTests extends ApiEndpointTests {
AbcApiTests() {
super("abc.com");
}
}
@Nested
class AbcdefApiTests extends ApiEndpointTests {
AbcdefApiTests() {
super("abcdef.com");
}
}
abstract static class ApiEndpointTests {
private final String pathPrefix;
ApiEndpointTests(String pathPrefix) {
this.pathPrefix = pathPrefix;
}
// existing test methods with annotations and all
Response getCustomerDataResponse(Object customer) {
Response getCustomerData = given().
header("Authorization", "Bearer " + token).
pathParam("customer", customer).
contentType(ContentType.JSON).
when().
get(pathPrefix + "/v1/customers/{customer}");
return getCustomerData;
}
}
}