spring-projects / spring-projects/spring-modulith

Unittest Scenario creates new database

Open
#1,197 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.2k
Forks
222
PR merge metrics
No merged PRs in 30d

Description

I use an in-memory H2 in Postgres mode with lower case names (as recommended by H2), defined in my src/main/resources/application.yml, which gets its schema definition via Liquibase:

# replaces application.yml in tests
spring:
  datasource:
    # no db name so each test gets its own db
    url: jdbc:h2:mem:;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH
    username: sa
    password:
  liquibase:
    change-log: classpath:/db/changelog/db.changelog-master.yml
    enabled: true

The test log shows that Liquibase defines the database schema, and my tests can use that database when interacting with my domain classes.

But when I trigger events using Scenario#publish, the corresponding @ApplicationModuleListener method gets called, yet in that context the database is empty. Error message:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Tabelle "mandant" nicht gefunden (diese Datenbank ist leer)
Table "mandant" not found (this database is empty); SQL statement:
SELECT "mandant"."id" AS "id", "mandant"."schluessel" AS "schluessel", "mandant"."bezeichnung" AS "bezeichnung" FROM "mandant" WHERE "mandant"."schluessel" = ? [42104-232]
	at org.h2.message.DbException.getJdbcSQLException(DbException.java:514) ~[h2-2.3.232.jar:2.3.232]

I know that application events for @ApplicationModuleListener methods are processed in a separate thread and transaction. But I am not sure if this can or should cause the entire schema definition to get rolled back.

So here is my question: How can I convince Scenario tests to use the same database as the test class where the scenario instance is used to publish events?

NOTE: I use an H2 database without database name because each test must use its own db. That is necessary since Liquibase executes this command for each test:

create table DATABASECHANGELOG

which fails starting from the second test with an error message that a table with lowercase name databasechangelog already exists.

It is possible that my setup leads to the described effects.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the test application.yml and the Scenario#publish path, then trace how @ApplicationModuleListener runs in its separate thread and transaction. Reproduce the issue with the nameless in-memory H2 URL and Liquibase schema setup. Done means Scenario tests can publish events while the listener sees the expected schema and database, with test isolation preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
database, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.