0

I have a medium sized spring boot application. When I run my tests in IntelliJ everything works fine.

My spring application loads a static set of test data from a testdata.sql file in a CommandLineRunner. This file creates the DB schema and loaded prepared data. This is very quick and works fine. That way my tests always run against the same set of data. (Test fixtures.)

But when I run my test with maven this fails. The first test runs perfectly fine. But the second test fails. It seems that spring-jpa tries to automatically generate a schema. Although I deactivated that in application-test.yml

Error Message and relevant logs from the maven run

DEBUG .(StartupInfoLogger.java:56).logStarting()                             | Running with Spring Boot v2.2.4.RELEASE, Spring v5.2.3.RELEASE
INFO  .(SpringApplication.java:655).logStartupProfileInfo()                  | The following profiles are active: test
INFO  .(RepositoryConfigurationDelegate.java:127).registerRepositoriesIn()   | Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[...]
INFO  .(TestDataCreator.java:242).run()                                      | ===== TestDataCreator: Loading schema and sample data from file: sampleDB-H2.sql

This works fine so far. Then the first test runs perfectly fine against that loaded test data. But then

The second test run fails

DEBUG .(StartupInfoLogger.java:56).logStarting()                             | Running with Spring Boot v2.2.4.RELEASE, Spring v5.2.3.RELEASE
INFO  .(SpringApplication.java:655).logStartupProfileInfo()                  | The following profiles are active: test
INFO  .(RepositoryConfigurationDelegate.java:127).registerRepositoriesIn()   | Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[...]
INFO  .(Dialect.java:172).<init>()                                           | HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
WARN  .(ExceptionHandlerLoggedImpl.java:27).handleException()                | GenerationTarget encountered exception accepting command : Error executing DDL "create sequence hibernate_sequence start with 1 increment by 1" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create sequence hibernate_sequence start with 1 increment by 1" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)

This is how my test classes look like

My Test classes do not have the @DirtiesContext annotation.

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class PollServiceTests  extends BaseTest { ... }

Why does hibernate generate a DDL here? This is not my TestDataCreator runnign a second time from what I can see from the logs.

src/main/resources/appliation-test.yml

spring:
  jpa:
    generate-ddl: false
    hibernate:
      ddl-auto: none

spring.active.profiles == test as can be seen in the logs. These settings are loaded.

I want all tests to run against that fixed dataset that should be loaded once at startup.

What am I missing? What is different when running the tests with maven

Robert
  • 1,579
  • 1
  • 21
  • 36

1 Answers1

0

After a lot of debugging I found the solution:

Deeply hidden in another application-<env>.yml file there still was a setting that made hibernate to create the database schema.

# In dev we can create a schema.sql script for initializing a database later in other environments
# https://stackoverflow.com/questions/37648395/how-to-see-the-schema-sql-ddl-in-spring-boot
# https://thoughts-on-java.org/standardized-schema-generation-data-loading-jpa-2-1/
javax:
  persistence:
    schema-generation:
      scripts:
        action: create
        create-target: build/my-db-schema.sql
      database:
        action: create    # Must be set if you want hibernate to create the actual schema in the DB in addition to the SQL file dump.

After commenting out this, everything works fine

Robert
  • 1,579
  • 1
  • 21
  • 36