spring bootmain
使用mysql
inapplication.properties
和test
使用h2
in application-ut.properties
。但是,在运行测试时,在控制台中会显示active profile: ut
,但test
仍使用datasource application.properties
. 一旦我删除application.properties
测试将使用h2
.
测试:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@ActiveProfiles(value = "ut")
聚甲醛:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
应用程序-ut.properties:
app.name=My app.
server.context-path=/
spring.jackson.serialization.indent-output=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.generate-ddl=true
应用程序属性:
app.name=my app
server.context-path=/
spring.jackson.serialization.indent-output=true
spring.datasource.url=jdbc:mysql://localhost:3306/myapp
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
控制台:
10:03:37.570 [main] INFO com.example.demo.RestControllerTest 641 logStartupProfileInfo - The following profiles are active: ut
10:03:37.726 [main] INFO org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext 582 prepareRefresh - Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6b53bcc2: startup date [Fri Jun 23 10:03:37 CST 2017]; root of context hierarchy
10:03:37.804 [background-preinit] INFO org.hibernate.validator.internal.util.Version 30 <clinit> - HV000001: Hibernate Validator 5.3.4.Final
10:03:38.225 [main] INFO org.springframework.data.repository.config.RepositoryConfigurationDelegate 165 multipleStoresDetected - Multiple Spring Data modules found, entering strict repository configuration mode!
**[And then it loads datasource in application.properties]**