Java Spring引导测试如何使用不同的application.properties进行集成测试
我有一个集成测试,但问题是,它使用来自主application.properties(mssql数据库)的数据源。在我的测试中,我想使用h2数据库,因为我已经在src/test/resources中创建了application-test.poperties。在我的测试类中,我定义了链接到此属性文件的@TestPropertySource。但是在日志输出中,我可以看到testclass仍然使用mssql数据库连接 这是我的测试课Java Spring引导测试如何使用不同的application.properties进行集成测试,java,spring-boot,spring-boot-test,Java,Spring Boot,Spring Boot Test,我有一个集成测试,但问题是,它使用来自主application.properties(mssql数据库)的数据源。在我的测试中,我想使用h2数据库,因为我已经在src/test/resources中创建了application-test.poperties。在我的测试类中,我定义了链接到此属性文件的@TestPropertySource。但是在日志输出中,我可以看到testclass仍然使用mssql数据库连接 这是我的测试课 @RunWith(SpringRunner.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")
这个
或纵断面注释
@ActiveProfiles("test")
尝试将其命名为application.properties,而不是application-test.properties。如果它在src/test/resources中,它应该自动覆盖src/main/resources中的一个
@ActiveProfiles("test")