Starting from:

$25

TQS - Lab 7 -  Integration tests -Solved

Test Containers, REST Assured
Context and key points
Key Points
While unit tests should focus on using the code with minimal dependencies on collaborators, integration tests focus on interactions and, as such, usually require additional test setup. The Test Containers framework makes it easy to deploy and run a container for the specific objective of running a test and then dispose it. The lifecycle of the container is integrated in the rest runner environment. A common use case is setting a dockerized database to run the test on a specific database.

The integration tests class also includes testing an API. Spring Boot offers a good support, as seen in Lab 3, but you can also resort to more generic testing libraries focused on REST API. A popular framework is RestAssured.

7.1 REST Assured  
RestAssured can be used as a REST-client library and we will use it in the context of integration testing, e.g.:

 

given(). 

        param("x", "y"). 

when(). 

        get("/lotto"). 

then().         statusCode(400).         body("lotto.lottoId", equalTo(6)); 

 

Consider that we want to “test” the https://jsonplaceholder.typicode.com/todos API.

a) Be sure to include the Rest-Assured dependencies in maven and that you are using the  these static imports as presented in the documentation. b) Create tests to verify that:

-       the endpoint to list all ToDos is available (status code 200)

-       when querying for ToDo #4, the API returns an object with title “et porro tempora”

-       When listing all “todos”, you get id #198 and #199 in the results.

 

Note: you may explore related examples from documentation or Baeldung.  

7.2 Cars
Consider the integration tests developed in Lab 3 for the Cars project.

Let us create a new version of the RestController test, using RestAssured.

  Create a new test with @WebMvcTest(CarController.class) to minimize the contexts loading.

 Since you are only loading the context for the controller, you should mock the required service implementation (e.g. @MockBean CarManagerService service; )

  RestAssured has a special support to work with Spring MockMvc. Be sure to add the following dependency:

<groupId>io.rest-assured</groupId> 

<artifactId>spring-mock-mvc</artifactId> <scope>test</scope> 

 

Then set a MockMvc instance that RestAssured will use when making requests:

                                  RestAssuredMockMvc.mockMvc(mockMvc); 

 

Invoke with:

RestAssuredMockMvc.given()…

 

Note that, in this example, we are using RestAssuredMockMvc instead of RestAssured. 

You do not provide a full URL since the MockMvc sliced environment is being used, not the full web environment (the test is faster).

7.3 Test Containers
Test Containers (TC) can be integrated in the test cases to prepare ephemeral docker containers required to run integration tests. A common scenario is to prepare a database server with a wellknown state.

TC are not specific to Spring Boot but there is a good support available, which we will use in this example.

 

a)     Create a simple SpringBoot project to manage a simple entity (e.g: Book, or Employee, or Student,…) to be persisted into a PostgreSQL database.

Suggested dependencies for Spring Initialzr      →  

b)    Be sure to create the entity and the repository.  

c)     Create an initialization script for the database in which you add a few sample entries.  

Suggestion: place the SQL file in

test/resources/db/migration/V001__INIT.sql to be picked automatically by Flyway (the Flyway dependency should have been declared).

 

d)    Create an integration test that prepares a postgres database, inserts some content and reads it back.  

Notes:   

-       you may learn from this code example, which is discussed in this video.

-       you may want to define a predefined execution order for the JUnit tests.

 

7.4 Cars with integration test
Let us create a different version of the full integration test (as in the last task in Lab 3), by using a database in a test container. In addition, use RestAssured as the REST client.

 

Be sure to:

-       launch the test in full web environment this time:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -          launch the database in a [test] container (e.g.: Postgres)

-       use RestAssured to invoke the API.

 

Note:

You may use a database initialization library or not:

-       you may use Flyway, for example, as in #7.3, adapting to this new scenario. An initialization script should be included in the project.

-       or you can rely on Spring Boot context loading tasks to initialize the database (as it normally does). Take note that addition configuration may be needed [in the test class], as exemplified in this base project [gs_cars_containers].  

 

 

 

More products