Develop_SP
Develop_SP

Reputation: 402

How to create DataJpaTest with Flyway migrations that use Postgres code?

I had have some difficulties to run my tests in spring boot app that have flyway to manage migrations, all tests up the applications and fail because the flyway try to run migrations.

I have installed H2 and use @DataJpaTest do try fix that, the application start normally but crash because the code that exists on the migration's file. Example:

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE app_user (
    id uuid DEFAULT uuid_generate_v4() NOT NULL PRIMARY KEY,
    name varchar(200) NOT NULL
);

As you can see all migrations file have postgres syntax, and the properties are hibernate: ddl-auto: validate

So have some way to only in tests create a H2 table from the @Entity without use migrations or all tests run without start spring application? If true can I test repositories without spring application?

Thanks

Upvotes: 1

Views: 1672

Answers (1)

Ralph
Ralph

Reputation: 120781

I would recommend to disable Flyway and enable Hibernate auto schema creation in the tests.

You can use properties to enable and disable features:

  • disable Flyway: spring.flyway.enabled=false
  • enable Hibernate Schema creation spring.jpa.hibernate.ddl-auto=create

For example

@SpringBootTest(properties = {
   "spring.flyway.enabled=false",
   "spring.jpa.hibernate.ddl-auto=create" })

Upvotes: 1

Related Questions