Spring Boot. @DataJpaTest H2 embedded database create schema

和自甴很熟 提交于 2019-12-01 15:09:48
David Canós

I had the same issue, I managed to resolve by creating schema.sql (in resources folder) with the content

CREATE SCHEMA IF NOT EXISTS <yourschema>

Documentation can be found here but imho the lack of real examples make it very complex. Warning: this script is also executed within the normal (not test) environment.

Not mandatory, but good practice, add h2 dependency only in test scope

<dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
   <scope>test</scope>
</dependency>

After couple hours of struggling I've found a workaround.

You can define spring.jpa.properties.hibernate.default_schema = DUMMY in your application.properties.

And then set spring.jpa.properties.hibernate.default_schema = in your test.properties and use together with @TestPropertySource("classpath:test.properties")

So, in this way the schema DUMMY won't be created and the entities will be created in default schema.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!