惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

Maciej Walkowiak - Java & Spring

Blog Generating HTTP clients in Spring Boot application from OpenAPI spec PostgreSQL and UUID as primary key Dynamic Projections with Spring Data JPA Container logs with Spring Boot and Testcontainers Reified Generics in Java? Running one-time jobs with Quartz and Spring Boot The best way to use Testcontainers with Spring Boot Spring Boot & Flyway - clear database between integration tests What's new in Spring? Activate Maven Profile by Operating System Spring Boot with Thymeleaf and Tailwind CSS - Complete Guide How to publish a Java library to Maven Central - Complete Guide Docker Compose - waiting until containers are ready Single file Java applications with JBang Beautiful bash scripts with Gum Running Java on CRaC How to log PostgreSQL queries with Testcontainers Spring Boot 3.0 & GraalVM Native Image - not a free lunch Creating Spring Cloud Function projects with AWS SAM Loading classpath resources to String with a custom JUnit extension Creating Project Templates with Cookiecutter Auto-Registering JUnit 5 extensions Spring Boot component scanning without annotations Listing Maven dependencies in Spring Boot Actuator Info endpoint Spring Cloud AWS 2.3 RC2 Released How I built vlad-cli - command line interface to Vlad Mihalcea The State of Java Relational Persistence On Choosing a Tech Stack
Faster integration tests with reusable Testcontainers and Flyway
2023-10-15 · via Maciej Walkowiak - Java & Spring
Published on
  • Testcontainers
  • Flyway
  • Spring Boot

INFO

Advice given in this article is especially relevant to applications with hundreds of Flyway migration files.

In Spring Boot & Flyway - clear database between integration tests I described how to ensure that each integration test starts with clean state - wipe out content from the database, and all database migrations applied from scratch. Such approach simplifies writing tests but comes with one significant drawback - running database migration takes time. The more migration files, the longer it takes. I still believe this is a solution worth considering for small services, but for large applications with hundreds or thousands of migrations, time needed to run migrations may become a biggest factor contributing to slow integration tests.

In this article I'll explore how reusable containers can significantly speed up integration tests during development phase and how to configure Flyway to use them efficiently.


Reusable Testcontainers ​

Containers started through Testcontainers typically are stopped either at the end of running a test class, or when all tests are finished at JVM shutdown. With database containers it means that before the actual test code runs it first waits for containers to start, then application context goes up - database migrations are executed, then the actual test runs. This process is repeated each time test is started from IDE, mvn test or gradle test is executed and as nobody likes waiting - it can significantly impact developer experience and productivity.

To address the waiting time for container to go up during development, Testcontainers team introduced a concept of reusable containers. When container is started in reusable mode, it does not get stopped when JVM shuts down unless it has been explicitly stopped.

This feature is relevant only for development time, not when running tests on CI:

Reusable containers are not suited for CI usage and as an experimental feature not all Testcontainers features are fully working (e.g., resource cleanup or networking). https://java.testcontainers.org/features/reuse/

While the main purpose of reusable containers is to remove the "Testcontainers start" phase for any subsequent test, it comes with an interesting side-effect for database containers and migrations - migrations are executed during first test run, for the second run the database is already up and migrations have been already executed - which removes also the "Flyway migrations" phase.

So far so good, the problem appears during development of a new database migration.

Flyway Migrations Problem ​

When Spring Boot starts, it calls Flyway#migrate method, that checks Flyway metadata table if all migrations have been already applied and if not, applies new ones. To ensure that the migrations code is exactly the one that has been executed on the database in previous run, it also validates migration files checksums. If checksums don't match, on Spring Boot startup you'll get an error like this:

console

org.flywaydb.core.api.exception.FlywayValidateException: Validate failed: Migrations have failed validation
Migration checksum mismatch for migration version 1.0
-> Applied to database : 1108600995
-> Resolved locally    : -1900419011
Either revert the changes to the migration, or run repair to update the schema history.

If something like this happens during production deployment it's a sign of a problem - existing migration file that has already been applied to the database has been modified - which should (almost) never happen. But it is very likely to happen during the development. Typical workflow for developing a change that includes changing the database structure is similar to:

  1. Database change is created as an migration file.
  2. Changes in Java code is implement.
  3. Ooops you realise you missed something and you need to modify migration file again.
  4. 💥 FlywayValidateException

One way to handle is connecting to the running database and deleting all its content. Not very convenient. A better way is to configure Spring Boot to do it for us only when migration fails with validation error.

Integration Tests Profile ​

Flyway configuration changes we are going to make must be applied only to integration tests. Make sure to set them in application.properties or test profile specific like application-test.properties placed in src/test/resources.

Enable Flyway Clean ​

By default, Flyway will prevent us from cleaning database with Flyway#clean method. Accidental use in production of such method would have disastrous effect. But it may be perfectly fine to use it during development or in tests. To enable a possibility to call Flyway#clean we must set spring.flyway.clean-disabled property to false.

application.propertiesapplication.yml

properties

spring.flyway.clean-disabled=false

yml

spring:
  flyway:
    clean-disabled: false

Enable Flyway Clean on Validation Error ​

In the next step we instruct Flyway to run Flyway#clean on application startup only when existing migration file has been modified and validation errors occur - by setting spring.flyway.clean-on-validation-error to true:

application.propertiesapplication.yml

properties

spring.flyway.clean-on-validation-error=true

yml

spring:
  flyway:
    clean-on-validation-error: true

Result ​

With reusable containers and Flyway configuration updated in place, the timeline looks like this:

On the first run we pay the price of containers going up and database migrations, but later in the future, migrations are not executed when not needed. This may not look like a big difference, but in some project it can translate into saving 1 minute or more for each test run. The shorter time to run tests, the more developers feel encouraged to write integration tests, not mentioning things like TDD workflow.

Conclusion ​

I hope you found this article useful. If you managed to shave off some seconds or minutes from executing your tests, or perhaps encountered some problems with this approach, drop a comment - I am curious to learn from your experiences!

Let's stay in touch and follow me on Twitter: @maciejwalkowiak

Subscribe to RSS feed