如何在spring-boot中使用testcontainers正确运行spock测试



我有一个spring-boot应用程序,测试使用spocktestcontainers(mysql(编写。我所做的工作很好,但感觉不太好(例如,因为每次测试迭代都使用@sql,所以我不得不在sql脚本中使用INSERT IGNORE ...。我也对静态和非静态mysqlcontainer的技巧感到不满意(。说到测试容器(实际上还有spock(,我是一个完全的初学者,所以如果有人能告诉我如何使用spock@sql、数据源和testcontainers让它变得更好,我将不胜感激。

@SpringBootTest
@ContextConfiguration(initializers = Initializer.class)
@Testcontainers
class GeneratorTest extends Specification {
public static MySQLContainer staticMySQLContainer = new MySQLContainer()
.withDatabaseName("test")
.withUsername("test")
.withPassword("test")
@Shared
public MySQLContainer mySQLContainer = mySQLContainer;
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
void initialize(@NotNull ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues values = TestPropertyValues.of(
"spring.datasource.url=" + staticMySQLContainer.getJdbcUrl(),
"spring.datasource.password=" + staticMySQLContainer.getPassword(),
"spring.datasource.username=" + staticMySQLContainer.getUsername()
)
values.applyTo(configurableApplicationContext)
}
}
@Autowired
private CarService carService
@Autowired
private BikeRepository bikeRepository
@Sql("/testdata/insert_into_cars.sql")
def "validate number of doors"(int carId, int expectedNrOfDoors) {
given:
Car car = carService.getById(carId)
expect:
car.getNrOfDoors() == expectedNrOfDoors
where:
carId || expectedNrOfDoors
1     || 3
2     || 3
3     || 5
}
}

更新(使用基于jdbc的容器(
当涉及到JDBC-based containers时,我不确定是否正确设置了它。我已经在test/resources目录中创建了application-test.properties。我放在那里:

spring.datasource.url=jdbc:tc:mysql:8.0.12://localhost:3306/shop?createDatabaseIfNotExist=true&serverTimezone=UTC
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver

我的测试类看起来像:

@SpringBootTest
@Testcontainers
@TestPropertySource(locations="classpath:application-test.properties")
class GeneratorTest extends Specification {
@Autowired
private CarService carService
<skipped for brevity>

当我尝试运行测试类时,我不断得到:

2018-11-20 19:10:25.409 2612@DESKTOP-MLK30PF  INFO --- [main] 🐳 [mysql:8.0.12].waitUntilContainerStarted : Waiting for database connection to become available at jdbc:mysql://192.168.99.100:32770/shop using query 'SELECT 1' (JdbcDatabaseContainer.java:128) 
2018-11-20 19:12:25.420 2612@DESKTOP-MLK30PF ERROR --- [main] 🐳 [mysql:8.0.12].tryStart : Could not start container (GenericContainer.java:297) 
org.rnorth.ducttape.TimeoutException: org.rnorth.ducttape.TimeoutException: java.util.concurrent.TimeoutException
at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:53)
at org.testcontainers.containers.JdbcDatabaseContainer.waitUntilContainerStarted(JdbcDatabaseContainer.java:129)
at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:292)

然后是

2018-11-20 19:12:25.486 2612@DESKTOP-MLK30PF  INFO --- [tc-okhttp-stream-242833949] 🐳 [mysql:8.0.12].accept : STDERR: 2018-11-20T18:10:44.918132Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.12'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL. (Slf4jLogConsumer.java:32) 
2018-11-20 19:12:25.487 2612@DESKTOP-MLK30PF  INFO --- [main] 🐳 [mysql:8.0.12].tryStart : Creating container for image: mysql:8.0.12 (GenericContainer.java:253) 
2018-11-20 19:12:25.609 2612@DESKTOP-MLK30PF  INFO --- [main] 🐳 [mysql:8.0.12].tryStart : Starting container with ID: de75c9dafed8032b84cb827bf43a29c1964bfe4e168422272c9310a4803fd856 (GenericContainer.java:266) 
2018-11-20 19:12:25.896 2612@DESKTOP-MLK30PF  INFO --- [main] 🐳 [mysql:8.0.12].tryStart : Container mysql:8.0.12 is starting: de75c9dafed8032b84cb827bf43a29c1964bfe4e168422272c9310a4803fd856 (GenericContainer.java:273) 
2018-11-20 19:12:25.901 2612@DESKTOP-MLK30PF  INFO --- [main] 🐳 [mysql:8.0.12].waitUntilContainerStarted : Waiting for database connection to become available at jdbc:mysql://192.168.99.100:32772/shop using query 'SELECT 1' (JdbcDatabaseContainer.java:128) 

关于mysql版本8的问题,请查看此处

UPDATE(已解决(:
@bsideup指出,基于jdbc的容器是此用例的最佳解决方案。正如我在上面所描述的那样,它运行得非常好,但我不得不将mysql版本从8更改为更低的版本。

您尝试过基于JDBC的容器吗?

您可以进行

def setupSpec(){ //or use setup(){ if not a @Shared Container
System.getProperties()
.putAll([
"spring.datasource.url"     : mySQLContainer...,
"spring.datasource.username": mySQLContainer...,
"spring.datasource.password": mySQLContainer...
])
}

然后你可以摆脱静态MySQLContainer

以及初始化程序

最新更新