how to implement ddt in api

If you use JUnit-5 in your framework, it allows to use 
@ParameterizedTest annotation beside @Test annotation. 
There are three different way to achieve DDT with @ParameterizedTest 
annotation. 

1. 	@ParameterizedTest
    @ValueSource(ints = {1,2,3,4})
    public void validateRegionNameTest1(int id) {
        given().pathParam("id", id)
                .when().get("/regions/{id}")	==> It will use 1,2,3,4 as 
                id in different runs...
                .prettyPeek()
                .then().assertThat().statusCode(200)
                .and().assertThat().body("region_id", equalTo(id));
    }

2.  @ParameterizedTest
    @CsvSource({
            "1, Europe",
            "2, Americas",
            "3, Asia",
            "4, Middle East and Africa"})
    public void validateRegionNameTest2(int id, String name) {  ==> iterate 
    each data set one by one 
    		given().pathParam("id", id)
                .when().get("/regions/{id}")
                .prettyPeek()
                .then().assertThat().statusCode(200)
                .and().assertThat().body("region_id", equalTo(id))
                .and().assertThat().body("region_name", equalTo(name));
    }

3.  @ParameterizedTest
    @CsvFileSource(resources = "/regions.csv") ==> uses external csv 
    file which is located under resources directory
    public void validateRegionNameTest3(int id, String name) {
        given().pathParam("id", id)
                .when().get("/regions/{id}")
                .prettyPeek()
                .then().assertThat().statusCode(200)
                .and().assertThat().body("region_id", equalTo(id))
                .and().assertThat().body("region_name", equalTo(name));
    }


3.67
3

                                    If you use JUnit-5 in your framework,
it allows to use @ParameterizedTest annotation
beside @Test annotation. 
There are different way to achieve 
DDT with @ParameterizedTest annotation.
(1-valueSource , 2- csvSource , 3-CsvFileSource)

1. 	@ParameterizedTest
    @ValueSource(ints = {1,2,3,4})
    public void validateRegionNameTest1(int id) {
        given().pathParam("id", id)
                .when().get("/regions/{id}")	==> It will use 1,2,3,4 as 
                id in different runs...
                .prettyPeek()
                .then().assertThat().statusCode(200)
                .and().assertThat().body("region_id", equalTo(id));
    }

2.  @ParameterizedTest
    @CsvSource({
            "1, Europe",
            "2, Americas",
            "3, Asia",
            "4, Middle East and Africa"})
    public void validateRegionNameTest2(int id, String name) {  ==> iterate 
    each data set one by one 
    		given().pathParam("id", id)
                .when().get("/regions/{id}")
                .prettyPeek()
                .then().assertThat().statusCode(200)
                .and().assertThat().body("region_id", equalTo(id))
                .and().assertThat().body("region_name", equalTo(name));
    }

3.  @ParameterizedTest
    @CsvFileSource(resources = "/regions.csv") ==> uses external csv 
    file which is located under resources directory
    public void validateRegionNameTest3(int id, String name) {
        given().pathParam("id", id)
                .when().get("/regions/{id}")
                .prettyPeek()
                .then().assertThat().statusCode(200)
                .and().assertThat().body("region_id", equalTo(id))
                .and().assertThat().body("region_name", equalTo(name));
    }

3.67 (3 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source