Jooq with testcontainer

Skip to first unread message

Viktor Konev

Apr 8, 2024, 2:35:01 AMApr 8
to jOOQ User Group

Hey everyone!

I'm relatively new to JOOQ and eager to find some guidance. I'm working with a stack that includes Kotlin, Spring Boot, Flyway, Testcontainers, and JOOQ.

Here's what I'm aiming to achieve:

  1. When I run the gradle build task, I want it to start a Testcontainer with an empty PostgreSQL database, then execute Flyway migrations, and finally, have JOOQ codegen generate Java/Kotlin code.
  2. When running tests, either keep the Testcontainer running (which might not be the best solution) or have it start only with the integration tests.
  3. Ultimately, I want everything related to containers to shut down properly.

I couldn't find a straightforward solution for this, but I did find something similar using docker-compose (before moving to tests).

Here's an example implementation:

BUT, the main issue arises when tests start running. JOOQ codegen tries to connect to the database again and fails because it wants to connect to port 5432, but in my tests, I intend to use TestContainers.

This setup might be overkill, and I'm confident there could be a simpler solution.

The whole idea is to be able to run a single command - gradle build, without any pre-existing databases, to handle migrations, generate JOOQ classes, and then run integration tests. Plus, I can't figure out how to separate these concepts, i.e., I want to keep the ability to run tests by themselves.

I realize my question is a bit sprawling, so thanks in advance for bearing with me and any help you can offer

Viktor Konev

Apr 8, 2024, 9:38:15 AMApr 8
to jOOQ User Group
Putting it simply, my ideal scenario involves using TestContainer (right now, I'm using docker-compose with PostgreSQL and Flyway CLI for migrations) to generate JOOQ classes alongside Flyway migrations. Moreover, I want my tests to work with these already generated JOOQ classes. However, the behavior during the gradle build confuses me because, for some reason, before running tests, JOOQ codegen attempts to connect to the database again. But since the port is random, it fails to do so. Essentially, I want the codegen either not to rerun before the tests or to use connection data from TestContainer.

понедельник, 8 апреля 2024 г. в 09:35:01 UTC+3, Viktor Konev:

Sergey Lappo

Apr 16, 2024, 12:59:43 PMApr 16
to jOOQ User Group
Hi, Viktor!

I think I've got a blog post describing exactly what you want to achieve:
- Spinning up the test containers
- Performing flyway migration
- Executing jOOQ codegen 
- potentially reusing the container for tests.
- Allowing Gradle to handle testcontainers lifecycle

If you have any questions - feel free to ask :) 
Reply all
Reply to author
0 new messages