Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@DataJpaTest ignores `spring.datasource.url` configuration. How to fix?

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

like image 381
Martin Mucha Avatar asked Dec 08 '22 12:12

Martin Mucha


1 Answers

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.

like image 75
Martin Mucha Avatar answered May 23 '23 08:05

Martin Mucha