【问题标题】:Failed to load application.properties file while using @DataJpaTest annotation使用 @DataJpaTest 注释时无法加载 application.properties 文件
【发布时间】:2018-04-17 20:47:05
【问题描述】:

我正在尝试在 Spring Boot 项目中加载应用程序属性以进行测试。我也在使用@DataJpaTest 注释。许多人建议将@TestPropertySource 注释与@datajpaTest 结合使用,但它不加载属性。如果我使用@SpringBooTest,它正在加载属性。

@RunWith(SpringRunner.class)
@DataJpaTest
@TestPropertySource(locations="classpath:application.properties")
public class EntityRepositoryTests {
}

我的应用程序属性文件位于 main/resource/ 文件夹中。 如果我使用@SpringBootTest(properties = { "classpath:application.properties" },它就可以工作,但我有

 @Autowired
    private TestEntityManager entityManager;

无法使用 SpringBootTest 进行自动配置。我也尝试过@ContextConfiguration(initializers=ConfigFileApplicationContextInitializer.class),但没有运气。

【问题讨论】:

    标签: spring unit-testing spring-data-jpa


    【解决方案1】:

    为了测试你必须使用

    @ActiveProfiles("test")
    

    连同 application-test.properties

    这些是我刚刚使用的

    @RunWith(SpringRunner.class)
    @DataJpaTest(showSql = false)
    @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
    @ActiveProfiles("test")
    

    【讨论】:

      猜你喜欢
      • 2019-12-12
      • 2015-12-20
      • 1970-01-01
      • 2018-01-08
      • 2016-07-30
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多