Spring-Boot @TestPropertySource 在与@SpringBootTest 一起使用时不加载外部属性文件
Spring-Boot @TestPropertySource not loading the external properties file when used with @SpringBootTest
我是 Spring Boot 的新手,我在使用 @TestPropertySource 注释和 @SpringBootTest 加载属性文件时遇到问题,这是我的代码
@RunWith(SpringRunner.class)
@SpringBootTest()
@TestPropertySource(locations="classpath:test_application.properties")
@Sql(value = {"/user_test_script.sql"})
@AutoConfigureTestDatabase(replace = Replace.NONE)
public class UserServiceImpleIntegrationTest {
@Autowired
private UserService userService;
@Autowired
ApplicationContext applicationContext;
@Test
public void testGetAllGuestUsers() throws IOException {
List<User> users =userService.getAllGuestUsers(0, 10);
assertThat(users).isNotNull();
assertThat(users).isNotEmpty();
assertThat(users).are(new Condition<User>() {
public boolean matches(User user) {
return user.getFirstName().contains("Guest"); }
});
}
}
这是我的 test_application.properties 文件内容
# Connection url for the database "test"
spring.datasource.url=jdbc:mysql://localhost:3306/test_db
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
# Hibernate
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
#LOGGING
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.springframework.web=ERROR
logging.level.org.hibernate=DEBUG
spring.profiles.active=junit
#TransactionManagement
logging.level.org.springframework.transaction.interceptor=TRACE
我的 classpath:test_application.properties 位于 src/test/resources 位置。
当我将相同的@TestPropertySource 注释与@DataJpaTest 一起使用时,正在加载属性文件,因为excepted.Here 是我的相同代码
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
@Sql(value = {"/item_test_script.sql" ,"/image_test_script.sql"})
@TestPropertySource(value="classpath:test_application.properties")
public class ItemRepoTest {
@Autowired
ItemRepo itemRepo;
@Test
public void testGetAllImagesByItemId() {
List<Image> images= itemRepo.getAllImagesByItemId(1l);
assertThat(images).isNotEmpty();
assertThat(images).size().isGreaterThanOrEqualTo(1);
}
}
非常奇怪的是如果我使用 properties 属性作为
@TestPropertySource(properties=
{"spring.datasource.username=root","spring.datasource.password=root"})
而不是 location 属性然后这些值被用来加载 database.what 我在这里缺少,或者什么配置是错误的。
尝试从注释中删除类路径,应该如下所示。
@TestPropertySource("test_application.properties")
@TestPropertySource
通常与@ContextConfiguration
结合使用
我在我的一个测试配置中有 @Configuration Class 而不是 @TestConfiguration 注释,所以 application.properties 文件被加载并替换了 test_application.properties 文件
我是 Spring Boot 的新手,我在使用 @TestPropertySource 注释和 @SpringBootTest 加载属性文件时遇到问题,这是我的代码
@RunWith(SpringRunner.class)
@SpringBootTest()
@TestPropertySource(locations="classpath:test_application.properties")
@Sql(value = {"/user_test_script.sql"})
@AutoConfigureTestDatabase(replace = Replace.NONE)
public class UserServiceImpleIntegrationTest {
@Autowired
private UserService userService;
@Autowired
ApplicationContext applicationContext;
@Test
public void testGetAllGuestUsers() throws IOException {
List<User> users =userService.getAllGuestUsers(0, 10);
assertThat(users).isNotNull();
assertThat(users).isNotEmpty();
assertThat(users).are(new Condition<User>() {
public boolean matches(User user) {
return user.getFirstName().contains("Guest"); }
});
}
}
这是我的 test_application.properties 文件内容
# Connection url for the database "test"
spring.datasource.url=jdbc:mysql://localhost:3306/test_db
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
# Hibernate
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
#LOGGING
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.springframework.web=ERROR
logging.level.org.hibernate=DEBUG
spring.profiles.active=junit
#TransactionManagement
logging.level.org.springframework.transaction.interceptor=TRACE
我的 classpath:test_application.properties 位于 src/test/resources 位置。
当我将相同的@TestPropertySource 注释与@DataJpaTest 一起使用时,正在加载属性文件,因为excepted.Here 是我的相同代码
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
@Sql(value = {"/item_test_script.sql" ,"/image_test_script.sql"})
@TestPropertySource(value="classpath:test_application.properties")
public class ItemRepoTest {
@Autowired
ItemRepo itemRepo;
@Test
public void testGetAllImagesByItemId() {
List<Image> images= itemRepo.getAllImagesByItemId(1l);
assertThat(images).isNotEmpty();
assertThat(images).size().isGreaterThanOrEqualTo(1);
}
}
非常奇怪的是如果我使用 properties 属性作为
@TestPropertySource(properties= {"spring.datasource.username=root","spring.datasource.password=root"})
而不是 location 属性然后这些值被用来加载 database.what 我在这里缺少,或者什么配置是错误的。
尝试从注释中删除类路径,应该如下所示。
@TestPropertySource("test_application.properties")
@TestPropertySource
通常与@ContextConfiguration
我在我的一个测试配置中有 @Configuration Class 而不是 @TestConfiguration 注释,所以 application.properties 文件被加载并替换了 test_application.properties 文件