【发布时间】:2020-02-09 03:35:48
【问题描述】:
我正在尝试在 Spring Boot 应用程序中使用 testcontainers 和 flyway 设置测试环境。所有这些都应该通过 DinD 方案运行。
当前测试示例如下:
import com.testapp.testapp.entity.TestEntity
import com.testapp.testapp.service.TestService
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Import
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.spock.Testcontainers
import spock.lang.Shared
import spock.lang.Specification
@Testcontainers
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@SpringBootTest
@Import([FlywayAutoConfiguration.class])
class ApplicationTests extends Specification {
@Shared
PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer()
.withDatabaseName("testdb")
.withUsername("postgres")
.withPassword("12345")
@Autowired
public TestService testappService;
@Test
def "entity_created"(){
given: "init test entity"
UUID uuid = UUID.randomUUID();
when: "create test entity"
def testEntity = TestEntity.builder()
.id(uuid.toString())
.description("Test")
.decimals(9)
.build()
testappService.createEntity(testEntity) // <- exception here, no schemas got created in testdb
then: "compare results"
}
}
application.properties文件的最终版本如下:
embedded.postgresql.enabled=true
embedded.postgresql.database=testdb
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver
spring.datasource.url=jdbc:tc:postgresql://${embedded.postgresql.host}:${embedded.postgresql.port}/${embedded.postgresql.database}
#spring.datasource.url=jdbc:tc:postgresql:///${embedded.postgresql.database}
spring.datasource.username=${embedded.postgresql.user}
spring.datasource.password=${embedded.postgresql.password}
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
spring.jpa.open-in-view=false
spring.flyway.enabled=true
spring.flyway.locations=classpath:resources/db.migration
所有可能的路径 spring.flyway.locations 都尝试过,但没有成功(通过 doc)
docker-compose.yml:
version: '2.4'
services:
testapp:
build: .
ports:
- "8080:8080"
environment:
POSTGRES_ADDRESS: jdbc:postgresql:///testdb
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 12345
networks:
- host_machine
restart: always
networks:
host_machine:
external: true
此测试的输出:
2019-10-11 18:06:52.862 INFO 11341 --- [ Test worker] ???? [postgres:9.6.12] : Creating container for image: postgres:9.6.12
2019-10-11 18:06:52.862 WARN 11341 --- [ Test worker] o.t.utility.RegistryAuthLocator : Failure when attempting to lookup auth config (dockerImageName: postgres:9.6.12, configFile: /home/user/.docker/config.json. Falling back to docker-java default behaviour. Exception message: /home/user/.docker/config.json (No such file or directory)
2019-10-11 18:06:52.900 INFO 11341 --- [ Test worker] ???? [postgres:9.6.12] : Starting container with ID: 54abc4af65619d0b5f913ccf4ff956e1439a26b7a494db2688691f9a25fde545
2019-10-11 18:06:53.266 INFO 11341 --- [ Test worker] ???? [postgres:9.6.12] : Container postgres:9.6.12 is starting: 54abc4af65619d0b5f913ccf4ff956e1439a26b7a494db2688691f9a25fde545
2019-10-11 18:06:56.818 INFO 11341 --- [ Test worker] ???? [postgres:9.6.12] : Container postgres:9.6.12 started
2019-10-11 18:06:56.869 INFO 11341 --- [ Test worker] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-10-11 18:06:56.923 INFO 11341 --- [ Test worker] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-10-11 18:06:56.961 INFO 11341 --- [ Test worker] org.hibernate.Version : HHH000412: Hibernate Core {5.3.7.Final}
2019-10-11 18:06:56.963 INFO 11341 --- [ Test worker] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-10-11 18:06:57.060 INFO 11341 --- [ Test worker] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-10-11 18:06:57.156 INFO 11341 --- [ Test worker] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
2019-10-11 18:06:57.172 INFO 11341 --- [ Test worker] o.h.e.j.e.i.LobCreatorBuilderImpl : HHH000422: Disabling contextual LOB creation as connection was null
2019-10-11 18:06:57.176 INFO 11341 --- [ Test worker] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@a097872
2019-10-11 18:06:57.578 INFO 11341 --- [ Test worker] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-10-11 18:06:58.076 INFO 11341 --- [ Test worker] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'asyncExecutor'
2019-10-11 18:06:58.691 INFO 11341 --- [ Test worker] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2019-10-11 18:06:58.859 INFO 11341 --- [ Test worker] c.testapp.testapp.ApplicationTests : Started ApplicationTests in 8.976 seconds (JVM running for 17.211)
2019-10-11 18:06:59.037 WARN 11341 --- [ Test worker] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42P01
2019-10-11 18:06:59.037 ERROR 11341 --- [ Test worker] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: relation "testschema" does not exist
Position: 376
2019-10-11 18:06:59.045 INFO 11341 --- [ Test worker] o.h.e.internal.DefaultLoadEventListener : HHH000327: Error performing load command : org.hibernate.exception.SQLGrammarException: could not extract ResultSet
但是,在没有测试的情况下启动应用程序时,相同的迁移工作正常,因此测试组件之间的某些交换问题。
我尝试了许多在谷歌中弹出的解决方法,但在我的情况下都没有真正奏效。
我是否应该自己应用某种迁移策略,或者可能存在一些可以快速修复的不明显的配置问题?
【问题讨论】:
标签: docker spock spring-boot-test testcontainers