简体   繁体   English

您可以在 Autoconfigured Spring Boot H2 测试数据库上设置兼容模式吗?

[英]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?有没有办法为 H2 数据库配置 H2 兼容模式,Spring Boot 可以自动配置以替换您的常规数据库而无需替换它?

There are documented ways of disabling the autoconfiguration test database replacement: https://stackoverflow.com/a/43557541/141042有记录禁用自动配置测试数据库替换的方法: 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 )如果您在测试类路径中添加application.properties ,它将在测试运行期间替换您的主application.properties ,因此您将无法维护两个文件(例如https://github.com/spring-projects/spring-boot/问题/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.我喜欢 Spring 引导自动配置测试数据库的简单性,但似乎我现在必须强制它进入 MySQL 兼容模式才能继续使用我现有的迁移。

So is there:有没有:

  1. a way to configure the compatibility mode of h2 when spring autoconfigures the test database without disabling that mechanism?当 spring 自动配置测试数据库而不禁用该机制时,一种配置 h2 兼容模式的方法?
  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 )一种为所有测试指定 jdbc url 的方法,而无需修改每个测试(例如包含配置文件)或维护两个应用程序属性文件(例如src/test/resources中的新application.properties

There isn't an option to set a custom URL for the embedded datasource that Spring Boot replaces in your tests.没有选项可以为 Spring Boot 在您的测试中替换的嵌入式数据源设置自定义 URL。 We offer a way to specify which connection type you want but that doesn't include the URL itself.我们提供了一种方法来指定您想要的连接类型,但不包括 URL 本身。 I have no idea how easy we could add that but it's worth looking at least, I've created issue #19038我不知道我们可以添加它有多容易,但至少值得一看,我已经创建了问题 #19038

As for specifying the URL, you shouldn't add an application.properties in your test classpath for the reason you've mentioned.至于指定 URL,由于您提到的原因,您不应该在测试类路径中添加application.properties The SO thread you've referenced already has an answer that refers to application-test.properties .您引用的 SO 线程已经有一个引用application-test.properties的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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