![]() ![]() Originally a special method annotated with was required to provide the connection credentials to the application context. ![]() Ideally this should exactly match the version that is also used in production. The proper container from Testcontainers is initialized with the given image "mysql:8.1", in our case the newest version of MySQL. Testcontainers setup to provide MySQL as a container MySQLContainer.withUrlParam("serverTimezone", "UTC") static final MySQLContainer mySQLContainer = new MySQLContainer("mysql:8.1") For this we extend our BaseIT.java with the following code. Now we want to configure a database that is exclusively available for our tests. MockMvc is configured automatically with the annotation AutoConfigureMockMvc - there are a lot of useful helpers included for calling our endpoints. If needed we can use this profile to activate special settings in our code.īy setting the variable webEnvironment to RANDOM_PORT the application will be started under a random and available port, so during testing there shouldn't be any conflict. With we define that the application is started with the profile "it". To write an integration test for our application, we start by adding the required = MyAppApplication.class, webEnvironment = abstract class BaseIT MockMvc mockMvc īy using the annotation, Spring Boot will load the full application context and thus enables us to make good integration tests. An embedded database would not be optimal for this, as there can be significant differences to the actual database, and therefore certain features may not work or errors may be missed.įor this purpose, there is the Testcontainers library to start a Docker image of exactly the database that is used by the developer and later in production. So in our Spring Boot context it would be great to examine the running app including the database (for example MySQL or PostgreSQL). BackgroundsĪn integration test is meant to test the interaction of multiple parts of the application. In this tutorial integration tests will be added to our Spring Boot application, connecting to the 'real' database using the Testcontainers library. Good software always includes automated tests to detect errors during the creation and modification of the code. Testcontainers setup together with generated test data is available in the Professional plan. Use the free Bootify Builder for starting your next Spring Boot app in no time - with custom database schema and best practices applied. Adding Integration Tests in Spring Boot with Testcontainers ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |