Spring-Boot @TestPropertySource 与 @SpringBootTest 一起使用时不加载外部属性文件

vic*_*cky 1 java spring spring-data-jpa spring-boot spring-boot-test

我是 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");  }
                 });
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 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
Run Code Online (Sandbox Code Playgroud)

我的 classpath:test_application.properties 位于 src/test/resources 位置。

当我将相同的 @TestPropertySource 注释与 @DataJpaTest 一起使用时,属性文件将作为例外加载。这是我的相同代码

@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);

    }

}
Run Code Online (Sandbox Code Playgroud)

非常奇怪的是如果我使用properties属性作为

@TestPropertySource(properties= {"spring.datasource.username=root","spring.datasource.password=root"})

而不是位置属性,然后使用这些值来加载数据库。我在这里缺少什么,或者什么配置是错误的。

vic*_*cky 5

我在我的测试配置类之一中有 @Configuration 而不是 @TestConfiguration Annotation ,因此 application.properties 文件被加载并替换 test_application.properties 文件