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

时间:2017-10-12 18:05:03

标签: java spring spring-boot spring-data-jpa 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");  }
                 });
    }
}

这是我的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位置。

当我在@DataJpaTest中使用相同的@TestPropertySource注释时,属性文件正在加载为例外。这是我的相同代码

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

    }

}

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

  

@TestPropertySource(属性=   {&#34; spring.datasource.username =根&#34;&#34; spring.datasource.password =根&#34;})

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

2 个答案:

答案 0 :(得分:2)

尝试从注释中删除classpath,应该如下所示。 f

@TestPropertySource("test_application.properties")通常与@TestPropertySource

一起使用

答案 1 :(得分:1)

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

相关问题