Spring启动测试如何使用不同的application.properties进行集成测试
Spring boot test how to use different application.properties for integration test
我有一个集成测试,但问题是,它使用来自主 application.properties 的数据源,它是一个 mssql 数据库。在我的测试中,我想使用 h2 数据库,因为我在 src/test/resources 中创建了一个 application-test.poperties 。在我的测试 class 中,我定义了链接到此 属性 文件的 @TestPropertySource。但是在日志输出中我可以看到 testclass 仍然使用 mssql 数据库连接。
这是我的测试class
@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")
我有一个集成测试,但问题是,它使用来自主 application.properties 的数据源,它是一个 mssql 数据库。在我的测试中,我想使用 h2 数据库,因为我在 src/test/resources 中创建了一个 application-test.poperties 。在我的测试 class 中,我定义了链接到此 属性 文件的 @TestPropertySource。但是在日志输出中我可以看到 testclass 仍然使用 mssql 数据库连接。
这是我的测试class
@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")