im_*_*ous 5 spock docker spring-boot-test testcontainers
我正在尝试在 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"
}
}
Run Code Online (Sandbox Code Playgroud)
文件最终版本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
Run Code Online (Sandbox Code Playgroud)
尝试了所有可能的路径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
Run Code Online (Sandbox Code Playgroud)
此测试的输出:
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
Run Code Online (Sandbox Code Playgroud)
然而,在没有测试的情况下启动应用程序时,相同的迁移工作正常,因此测试组件之间的某些交换存在问题。
我已经尝试了谷歌中出现的许多解决方法,但对我来说没有任何效果。
我是否应该自己应用某种迁移策略,或者可能存在一些可以快速修复的不明显的配置问题?
经过深入调查后,需要将以下行添加到属性中:
spring.jpa.hibernate.ddl-auto=无
默认情况下,集成测试会从迁移中删除所有数据。添加上面的单独的以避免它。
| 归档时间: |
|
| 查看次数: |
5372 次 |
| 最近记录: |