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

Comment From: philwebb

This is an edge case that we've not considered. You might be able to work around it by overriding the spring.datasource.schema-username property to "" in your test.

Comment From: eddumelendez

I have added my findings in https://github.com/spring-projects/spring-boot/pull/19327#issuecomment-562846662

Comment From: wilkinsona

Closing in favour of #19327.

Comment From: snicoll

We're going to give it another go by overriding spring.datasource.schema-username when we replace the DataSource.

Comment From: somayaj

Can this be worked?

Comment From: somayaj

The !isEmbedded boolean check added to the username and password might solve the issue here

Comment From: wilkinsona

This appears to work, but I'd like some more eyes on it before merging it.

Comment From: somayaj

@wilkinsona I haven't created a PR yet. will do so if you think its a viable fix.

Comment From: wilkinsona

Thanks for the offer, @somayaj, but if the team agree that my proposed changes seem reasonable we won't need a PR for this one.

Comment From: snicoll

LGTM Andy. Thanks!