Skip to content

@DataJpaTest using H2 with schema.sql and spring.datasource.schema-username fails #19321

@silovmar

Description

@silovmar

Version: Spring Boot v2.2.1.RELEASE, Spring v5.2.1.RELEASE

Hi, I'm using @DataJpaTest with default embedded H2 DB for testing. Also I'm creating DB schema using schema.sql which contains CREATE SCHEMA IF NOT EXISTS TEST_SCHEMA;.

Unit tests annotated with the @DataJpaTest ran fine until I added to my production application.properties properties spring.datasource.schema-username=${DB_USERNAME_SCH} and spring.datasource.schema-password=${DB_PASSWORD_SCH}.

This causes that a datasource based on the application.properties is trying to be created instead of the default H2 database. The creation of DB from application.properties fails because the datasource configuration looks like this:

spring.datasource.driver-class-name= oracle.jdbc.OracleDriver
spring.datasource.url= ${DB_URL}
spring.datasource.username= ${DB_USERNAME}
spring.datasource.password= ${DB_PASSWORD}
spring.datasource.schema-username= ${DB_USERNAME_SCH}
spring.datasource.schema-password= ${DB_PASSWORD_SCH}

I would expect that the embedded H2 is used even if the schema-username and schema-password is used.

Source code which caueses the problem:
org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java:197 which is called from org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java:101

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions