Skip to content

Using EmbeddedPostgres to test Spring Data JPA repositories #296

@slwcz

Description

@slwcz

Hi,
I am developing an application which uses Spring Data JPA and Postgres DB. Now when writing integration tests, I am struggling how to properly implement the test because I am always hitting the real database and not the embedded one.
Under src/main/resources/META-INF I have persistence.xml referring to the real database. I am using RESOURCE_LOCAL because it is a standalone application. Therefore I dont have any datasource declared.
When the initialization of my integration test happens, JPA repository is auto-discovered, LocalEntityManagerFactoryBean initialized which boils down to reading the ersistence.xml and initializing the connection to the real DB. I also see in the log of the integration test, that embedded Postgres is initialized but in fact "the JPA repository under test" queries the real DB which is not intended.
My question is:

  • is my scenario supported?
  • how can I bypass the persistence.xml parsing and initialization provided that there is no datasource in my Spring context. Can I duplicate persistence.xml to src/test/resources? How would the JDBC URL pointing at the embedded Postgres look like?

Thank you.
Slavek

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions