11

I had issues with @DataJpaTest, because tables fail to be created, due to missing schema [1]. So I thought, I could cheat spring by creating schema in connection string as in:

application.properties

spring.datasource.url=jdbc:h2:mem:SCH;MODE=Oracle;DB_CLOSE_DELAY=-1;INIT=create schema if not exists SCH
spring.datasource.username=test
spring.datasource.password=test

however mysteriously, that did not help. Why?

quick peek at excerpt from test log:

2019-02-13 17:48:12.735  INFO [CCH,,,] 28586 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:h2:mem:11247702-6bc4-44b9-be65-9639ebb8d695;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'

well it's because @DataJpaTest ignores datasource configured in application.properties and rather uses autoconfigured h2 database

I found that I can use @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) but that does not work either and fails with Unknown host specified. What host???

So any ideas how to confince @DataJpaTest to use configured data source?

[1] Using @DataJpaTest I cannot force h2 to create schema

3
  • Do you have Oracle jars in your pom dependencies? If not, they won't be configured as data-source is not on the classpath. Commented Feb 13, 2019 at 21:28
  • 1
    MODE=Oracle actually works even if oracle driver is not on class path, but thanks for hint. I retest everything, and actually found out, that it was my bad. Given @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) actually turns off autoconfigured embedded h2 database and uses the one declared in project configuration. Sorry for my mistake. Commented Feb 14, 2019 at 9:07
  • For me it was a dependency error stackoverflow.com/questions/55677084 Commented Apr 14, 2019 at 16:59

1 Answer 1

26

I apologize, it was my mistake in project/tests configuration.

To give some answer: By default @DataJpaTest uses embeded h2 databaze and ignores the connection string declared in application.properties. Annotation @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) disables this behavior, and db configured in application.properties will be used by @DataJpaTest test.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.