我有一个 spring boot 应用程序,使用spock
and testcontainers
(mysql) 编写测试。我所做的工作正常,但感觉不对(因为@sql
每次测试迭代都会进行,所以我必须INSERT IGNORE ...
在我的 sql 脚本中使用。我也不满意静态和非静态 mysqlcontainer 的技巧) . 当谈到 testcontainers(实际上是 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
我不确定我是否正确设置它时。我已经application-test.properties
在test/resources
目录中创建了。我放在那里:
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
看这里
更新(已解决):
正如@bsideup 指出的那样,基于 jdbc 的容器是此用例的最佳解决方案。正如我上面描述的那样,它工作得很好,但我不得不将 mysql 版本从更改8
为更低。