Maven
Örnek
Şöyle yaparız
<dependency><groupId>org.testcontainers</groupId><artifactId>mysql</artifactId><scope>test</scope></dependency>
Örnek
Şöyle yaparız
testImplementation("org.testcontainers:mysql") testImplementation("org.testcontainers:spock") testImplementation("org.testcontainers:testcontainers")
Örnek
Şöyle yaparız
datasources:
default:
url: jdbc:tc:mysql:8:///db
driverClassName: org.testcontainers.jdbc.ContainerDatabaseDrivertestcontainers:
reuse:
enable: trueFinally, in order to speed the testing process up, we should try to set a level of isolation in our tests that allow us to reuse the same image, which will reduce our test time dramatically:
MySQLContainer Sınıfı
Örnek
Şöyle yaparız
public static MySQLContainer mySQLContainer = new MySQLContainer("mysql:5.7") .withUsername("username") .withPassword("password") .withDatabaseName("test_db");
Şöyle yaparız. Burada normalde container sınıfının start() metodunu çağırmaya gerek yok.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
@Testcontainers
class OrderControllerTest {
static final MySQLContainer MY_SQL_CONTAINER;
@Autowired
WebTestClient webTestClient;
@Autowired
private OrderEntityRepository orderEntityRepository;
static {
MY_SQL_CONTAINER = new MySQLContainer("mysql:latest");
MY_SQL_CONTAINER.start();
}
@DynamicPropertySource
static void configureTestProperties(DynamicPropertyRegistry registry){
registry.add("spring.datasource.url",() -> MY_SQL_CONTAINER.getJdbcUrl());
registry.add("spring.datasource.username",() -> MY_SQL_CONTAINER.getUsername());
registry.add("spring.datasource.password",() -> MY_SQL_CONTAINER.getPassword());
registry.add("spring.jpa.hibernate.ddl-auto",() -> "create");
}
...
}
Hiç yorum yok:
Yorum Gönder