简体   繁体   中英

Can you set the compatibility mode on the Autoconfigured Spring Boot H2 test DB?

Is there a way to configure the H2 Compatibility Mode for the H2 Database that Spring Boot can auto configure to replace your regular database without just replacing it?

There are documented ways of disabling the autoconfiguration test database replacement: https://stackoverflow.com/a/43557541/141042

I don't mind doing something like this, but most of the alternatives come with other complexities:

  • if you add a application.properties in your test classpath, this replaces your main application.properties during test runs, so then you're stuck maintaining two files (eg https://github.com/spring-projects/spring-boot/issues/10271 )
  • if you set up a profile for test runs, then you have to make sure that any test needing the test database is marked with the profile

Is there a better way of doing this? I like the simplicity of the Spring Boot auto configured test database, but it seems like I have to force it into MySQL compatibility mode now to continue to work with my existing migrations.

So is there:

  1. a way to configure the compatibility mode of h2 when spring autoconfigures the test database without disabling that mechanism?
  2. a way of specifying the jdbc url for all tests without having to modify each test (eg to include a profile) or maintaining two application property files (eg a new application.properties in src/test/resources )

There isn't an option to set a custom URL for the embedded datasource that Spring Boot replaces in your tests. We offer a way to specify which connection type you want but that doesn't include the URL itself. I have no idea how easy we could add that but it's worth looking at least, I've created issue #19038

As for specifying the URL, you shouldn't add an application.properties in your test classpath for the reason you've mentioned. The SO thread you've referenced already has an answer that refers to application-test.properties .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM