簡體   English   中英

Spring Boot測試如何使用不同的application.properties進行集成測試

[英]Spring boot test how to use different application.properties for integration test

我有一個集成測試,但是問題是,它使用來自主應用程序.properties的數據源,該文件是mssql數據庫。 在測試中,我想使用h2數據庫,在src / test / resources中創建了一個application-test.poperties。 在測試類中,我定義了@TestPropertySource,它鏈接到該屬性文件。 但是在日志輸出中,我可以看到testclass仍然使用mssql數據庫連接。

這是我的考試課

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    @Transactional
    @TestPropertySource(locations="classpath:application-test.properties")
    public class UserControllerTest {

        @LocalServerPort
        private int port;
        TestRestTemplate restTemplate = new TestRestTemplate();
        HttpHeaders headers = new HttpHeaders();
 ...

這是我的src / test / resources / application-test.properties文件

    spring.datasource.datasource.url=jdbc:h2:mem:scserver
    spring.datasource.username=sa
    spring.datasource.password=
    spring.datasource.driverClassName=org.h2.Driver
    spring.datasource.jpa.database-platform=org.hibernate.dialect.H2Dialect
   spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect

    # Hibernate ddl auto (create, create-drop, validate, update)
    spring.jpa.hibernate.ddl-auto=create-drop


    #logging
    logging.level.root=info
    logging.file=foo-spring-rest.log

    #required for SpringBootTest does not know why
    spring.main.allow-bean-definition-overriding=true
    spring.h2.console.enabled=true
    spring.h2.console.path=/h2-console

嘗試使用代替

@TestPropertySource(locations="classpath:application-test.properties")

這個

@TestPropertySource(locations = {"classpath:application-test.properties"})
// dont forget the curvy brackets, because location'S' 

或個人資料注釋

@ActiveProfiles("test")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM