Java 在spring boot中使用嵌入式数据库进行测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42369467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use embedded database for test in spring boot
提问by Leonid Bor
I have a spring boot application, it has a couple of @Entity
classes and @RepositoryRestResource
repositort interfaces for them. Now I want to write some tests, where I can check that I can add a new record into my database using those repositories, but I don't want to use my configured MySQL database for it, but instead I want to use some embedded db like H2. At the moment I have an application.properties
file, which looks like this:
我有一个 Spring Boot 应用程序,它有几个@Entity
类和@RepositoryRestResource
它们的存储库接口。现在我想写一些测试,在那里我可以检查我是否可以使用这些存储库将新记录添加到我的数据库中,但我不想使用我配置的 MySQL 数据库,而是我想使用一些嵌入式数据库像H2。目前我有一个application.properties
文件,它看起来像这样:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=qwerty123
Question:How do I configure my app to use other db for tests? I have no xml in my project, everything is based on annotations. I tried to define @Configuration
class with @Bean
to create DataSource
and then use it with @ContextConfiguration
annotation on test class, but it says that it can't load context.
问题:如何配置我的应用程序以使用其他数据库进行测试?我的项目中没有 xml,一切都基于注释。我试图定义@Configuration
类 @Bean
来创建DataSource
,然后@ContextConfiguration
在测试类上使用注释来使用它,但它说它无法加载上下文。
采纳答案by Boni García
If you are using a Maven project, you can add a application.properties
file into your src/test/resources
, for example with the following content.
如果您使用的是 Maven 项目,则可以将application.properties
文件添加到您的 中src/test/resources
,例如具有以下内容。
# Create DDL
spring.jpa.hibernate.ddl-auto=create
# H2 in local file system allowing other simultaneous connections
spring.datasource.url=jdbc:h2:~/test;AUTO_SERVER=TRUE
Also, you need to include H2 as dependency (pom.xml
):
此外,您需要将 H2 作为依赖项 ( pom.xml
)包含在内:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>
回答by Quinton Delpeche
You will need to use Spring Profiles - https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles
您将需要使用 Spring Profiles - https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles
You will define an active profile using "spring.profiles.active = development" and then including H2 in your development profile.
您将使用“spring.profiles.active = development”定义一个活动配置文件,然后在您的开发配置文件中包含 H2。
The examples use YAML, but they work in standard properties files as well.
这些示例使用 YAML,但它们也适用于标准属性文件。
回答by yuranos
Spring Boot provides 2 magic annotations related to JPA autoconfigs for tests: @DataJpaTest
and @AutoConfigureTestDatabase
.
The javadoc says:
Spring Boot 为测试提供了 2 个与 JPA 自动配置相关的魔法注释:@DataJpaTest
和@AutoConfigureTestDatabase
. javadoc 说:
By default, tests annotated with @DataJpaTest will use an embedded in-memory database (replacing any explicit or usually auto-configured DataSource). The @AutoConfigureTestDatabase annotation can be used to override these settings.
If you are looking to load your full application configuration, but use an embedded database, you should consider @SpringBootTest combined with @AutoConfigureTestDatabase rather than this annotation.
默认情况下,用@DataJpaTest 注释的测试将使用嵌入式内存数据库(替换任何显式或通常自动配置的数据源)。@AutoConfigureTestDatabase 注释可用于覆盖这些设置。
如果您希望加载完整的应用程序配置,但使用嵌入式数据库,则应考虑将 @SpringBootTest 与 @AutoConfigureTestDatabase 结合使用,而不是此注释。
So, the only thing you absolutely need is a dependency in your pom file:
因此,您唯一绝对需要的是 pom 文件中的依赖项:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
That's it. However, spring boot spec also has 2 useful caveats:
就是这样。但是,spring boot 规范也有两个有用的警告:
You need not provide any connection URLs. You need only include a build dependency to the embedded database that you want to use. If you are using this feature in your tests, you may notice that the same database is reused by your whole test suite regardless of the number of application contexts that you use. If you want to make sure that each context has a separate embedded database, you should set spring.datasource.generate-unique-name to true.
您无需提供任何连接 URL。您只需要包含对要使用的嵌入式数据库的构建依赖项。如果您在测试中使用此功能,您可能会注意到,无论您使用多少应用程序上下文,整个测试套件都会重用同一个数据库。如果要确保每个上下文都有单独的嵌入式数据库,则应将 spring.datasource.generate-unique-name 设置为 true。
And another one:
还有一个:
If, for whatever reason, you do configure the connection URL for an embedded database, take care to ensure that the database's automatic shutdown is disabled. If you use H2, you should use DB_CLOSE_ON_EXIT=FALSE to do so. If you use HSQLDB, you should ensure that shutdown=true is not used. Disabling the database's automatic shutdown lets Spring Boot control when the database is closed, thereby ensuring that it happens once access to the database is no longer needed.
如果出于某种原因,您确实为嵌入式数据库配置了连接 URL,请注意确保禁用数据库的自动关闭。如果使用 H2,则应使用 DB_CLOSE_ON_EXIT=FALSE 来执行此操作。如果使用 HSQLDB,则应确保不使用 shutdown=true。禁用数据库的自动关闭可以让Spring Boot 控制何时关闭数据库,从而确保在不再需要访问数据库时发生这种情况。
That's almost all you need to know about Spring Boot and embedded DBs. I see absolutely no reason to use the scope of dependency other than test
, unless you actually intentionally configure an embedded DB for your application runtime. Believe it or not H2 jar alone takes 1.8M inside your fat jar. In the world on granular microservices, serverless and lambda functions it does matter what you put inside your apps.
这几乎就是您需要了解的有关 Spring Boot 和嵌入式 DB 的全部内容。我认为绝对没有理由使用除 之外的依赖范围test
,除非您实际上有意为应用程序运行时配置嵌入式数据库。信不信由你 H2 jar 在你的胖罐子里需要 1.8M。在粒度微服务、无服务器和 lambda 函数的世界中,您在应用程序中放入的内容很重要。
I would also recommend checking the options in @AutoConfigureTestDatabase. I use it with @SpringBootTest, but it can also be used with some other annotations, namely @DataJpaTest, both mentioned above:
我还建议检查@AutoConfigureTestDatabase 中的选项。我将它与@SpringBootTest 一起使用,但它也可以与其他一些注释一起使用,即@DataJpaTest,两者都在上面提到过: