admin管理员组

文章数量:1240593

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/v1/customers/{customer}");

        return getCustomerData;
    }
}

I want to run the entire class of tests once with "abc/v1/customers/{customer}" and after that with "abcdef/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.

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/v1/customers/{customer}");

        return getCustomerData;
    }
}

I want to run the entire class of tests once with "abc/v1/customers/{customer}" and after that with "abcdef/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.

Share Improve this question edited yesterday jonrsharpe 122k30 gold badges267 silver badges474 bronze badges asked yesterday elagelag 111 bronze badge 6
  • How do you configure the domain names? If it's in a config file, can you have your build script replace that setting and run the tests again? – Robert Commented yesterday
  • You could, for example, use @MethodSource instead and provide more than one parameter to the test, so you can pass both the URL and the value to getCustomerDataResponse(). – JustAnotherDeveloper Commented yesterday
  • Use a second @ValueSource for each one to pass an additional param of the base url – scrhartley Commented yesterday
  • @JustAnotherDeveloper This implies that I will need to update all of my parameterized tests, which I was hoping to avoid. – elag Commented yesterday
  • 1 Have you thought about making two subclasses of your ApiTests class, with different implementations of getCustomerDataResponse? You could then run JUnit on each subclass. It's an ugly solution, but I think it will do what you want. – Dawood ibn Kareem Commented yesterday
 |  Show 1 more comment

1 Answer 1

Reset to default 0

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

    @Nested
    class AbcdefApiTests extends ApiEndpointTests {
        AbcdefApiTests() {
            super("abcdef");
        }
    }

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

本文标签: javaHow to run a test class twice with different class parametersStack Overflow