Skip to content

Unittest Scenario creates new database #1197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dschulten opened this issue May 20, 2025 · 0 comments
Open

Unittest Scenario creates new database #1197

dschulten opened this issue May 20, 2025 · 0 comments

Comments

@dschulten
Copy link

dschulten commented May 20, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant