EPUBstating, "Provide lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container." A prompt on the right says, "Press ⌘ for multiple adds." Figure 10-3 Adding Testcontainers dependency Annotate Test Classes Annotate your test classes with @SpringBootTest to enable Spring Boot context loading for tests and @Testcontainers to activate Testcontainers support. @Testcontainers @SpringBootTest class DemoApplicationTests { } Container Initialization Create container instances in your test classes using Testcontainers’ utilities. For example, if you want to spin up an in-memory Redis cache instance: @Testcontainers @SpringBootTest class DemoApplicationTests { @Container @ServiceConnection static RedisContainer container = new RedisContainer(RedisContainer.DEFAULT_IMAGE_NAME); @Test void myTest() { System.out.println(container.isRunning()); System.out.println(container.getRedisURI()); } } This code snippet demonstrates the integration of Testcontainers with Spring Boot, showcasing how to use a Redis container in a test scenario. Here’s a breakdown of what each part of the code does: @Testcontainers : This annotation indicates that this test class will utilize Testcontainers. It allows us to manage the lifecycle of containers used within the test class. @SpringBootTest : Indicates that this is a Spring Boot test. It loads the complete application context and allows integration testing with Spring components. @Container :
Rate this eBook
| File Name: | fe716032c547485155b1c9ce124af8f8.epub |
| Original Title: | When Docker Meets Java |
| Creator / Author: | Ashish Choudhary |
| Language: | English (en) |
| Identifier: | ASIN:306036 |
| Publisher: | Ashish Choudhary Publishing |
| Category: | Fiction |
| Format: | 📗 EPUB eBook |
| File Size: | 3.5 MB |