![](/img/trans.png)
[英]How to add the mode=mysql to embedded H2 DB in Spring Boot 1.4.1 for @DataJpaTest?
[英]Spring Boot DataJpaTest unit test reverting to H2 instead of mySql
我有一個簡單的“hello world”Spring Boot 應用程序。 它有一個實體(“IssueReport”)並且它被配置為運行 mySQL(而不是默認的 H2 嵌入式數據庫)。
該應用程序本身運行良好。 我創建了一個 mySql 數據庫和用戶,Spring Boot/Hibernate 創建了表並在我運行應用程序時成功填充和讀取了 mySQL 數據。 生活是美好的 - mySQL 和我的 Spring Boot 應用程序本身沒有問題。
問:現在如何在單元測試中使用 mySQL(而不是嵌入式 H2)?
我創建了第二個單獨的 mySQL 數據庫: test2_test_db
。
我正在使用 Spring Boot 2.0.6; STS 3.9.6 上的 Eclipse Photon; Ubuntu Linux。
我在src/test/resources/
創建了application-test.properties
:
spring.datasource.url=jdbc:mysql://localhost:3306/test2_test_db spring.datasource.username=springuser spring.datasource.password=springuser spring.jpa.hibernate.ddl-auto=create
這是整個單元測試:
package com.hellospring.example; import static org.assertj.core.api.Assertions.assertThat; import java.util.List; import javax.transaction.Transactional; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import com.hellospring.example.entity.IssueReport; import com.hellospring.example.repositories.IssueRepository; @RunWith(SpringRunner.class) @ActiveProfiles("test") @Transactional @DataJpaTest public class IssueRepositoryIntegrationTests { @Autowired private TestEntityManager entityManager; @Autowired private IssueRepository issueRepository; @Test public void addNewIssue() { System.out.println("addNewIssue()..."); // <-- This prints in the console final String email = "test@test.io"; List<IssueReport> resultSet = issueRepository.findAll(); // <-- We get an exception in here... } }
這是控制台錯誤:
2018-10-25 22:20:16.381 INFO 13637 --- [ main] cveIssueRepositoryIntegrationTests : The following profiles are active: test 2018-10-25 22:20:16.405 INFO 13637 --- [ main] scaAnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@d554c5f: startup date [Thu Oct 25 22:20:16 PDT 2018]; root of context hierarchy 2018-10-25 22:20:17.059 INFO 13637 --- [ main] beddedDataSourceBeanFactoryPostProcessor : Replacing 'dataSource' DataSource bean with embedded version 2018-10-25 22:20:17.060 INFO 13637 --- [ main] osbfsDefaultListableBeanFactory : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] with [Root bean: class [org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] 2018-10-25 22:20:17.308 INFO 13637 --- [ main] osjdeEmbeddedDatabaseFactory : Starting embedded database: url='jdbc:h2:mem:979b3ce9-604e-4efd-a6d4-79576c3d67e9;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa' 2018-10-25 22:20:17.685 INFO 13637 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default' ... <= I do *NOT* want H2! I want mySQL! 2018-10-25 22:20:19.315 WARN 13637 --- [ main] ohengine.jdbc.spi.SqlExceptionHelper : SQL Error: 42102, SQLState: 42S02 2018-10-25 22:20:19.316 ERROR 13637 --- [ main] ohengine.jdbc.spi.SqlExceptionHelper : Table "ISSUE_REPORT" not found; SQL statement: ... <= Here's the exception from running the test...
問:最簡單的更改是什么,以便我可以使用 mySQL 運行我的單元測試,就像我可以使用 mySQL 運行我的 Spring Boot 應用程序一樣?
問:“@DataJpaTest”是這里的最佳選擇,還是應該嘗試不同的注釋?
問:我必須創建一個單獨的“Bean”類嗎? 如果是這樣,你能舉個例子嗎?
================================================== ==============
感謝您的所有回復。 包括 Simon Martinelli(現已刪除)的回復。
解析度:
我原來的application-test.properties
沒有問題。
我把它放在錯誤的地方:任何配置文件的所有application.properties 文件通常應該放在同一個項目文件夾中: src/main/resources
<= 示例: src/main/resources/application-test.properties
@Transactional
在這里@Transactional
- 我刪除了它。 我保留了它@ActiveProfiles("test")
。
根據 Karthik R 的建議,我添加了@AutoConfigureTestDatabase(replace=Replace.NONE)
。
此時,測試成功讀取application-test.properties
並使用 MySQL 而不是 H2。
最終注釋:
@RunWith(SpringRunner.class) @ActiveProfiles("test") @DataJpaTest @AutoConfigureTestDatabase(replace=Replace.NONE) public class IssueRepositoryIntegrationTests {
我發現此鏈接特別有用: Spring Boot – 基於配置文件的屬性和 yaml 示例
<= 我總是發現http://www.mkyong.com上的所有材料都非常好!
默認情況下, @DataJpaTest
使用內存 H2 數據庫進行 repo 測試。 如果您需要使用實際的數據庫,您可以考慮禁用自動配置或使用@SpringBootTest
啟用整個應用程序 web mvc。
要禁用自動配置:
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@Transactional
@DataJpaTest
@AutoConfigureTestDatabase(replace=Replace.NONE)
public class IssueRepositoryIntegrationTests
@AutoConfigureTestDatabase
為您配置測試 H2 DB。 您可以在上面特別提到不要,或者您可以將此自動配置排除為:
@EnableAutoConfiguration(exclude=AutoConfigureTestDatabase.class)
PS::我自己還沒有嘗試過上述排除。
有關更多信息,請訪問 javadoc: https : //docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabase.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.