java - Spring JUnit .properties 文件不起作用

标签 java spring jpa jdbc junit

这是我的测试类中的注释配置。

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource("classpath:jdbc.properties")
@ContextConfiguration({"classpath:applicationContext.xml","classpath:mvc-dispatcher-servlet.xml"})
@WebAppConfiguration

applictionCotnext.xml包括jpa-context.xml ,数据库配置位于 jpa-context.xml ,以及由 jdbc.properties 配置的数据源.

当我运行测试方法时,由于 jdbc.properties 中的所有值均为空而导致无法打开连接。

然后我在同一测试类中添加一个用于访问属性的方法。

@Inject
private ApplicationContext context;
@Test
public void accessProperties(){
    MutablePropertySources sources = ((ConfigurableApplicationContext) context)
                .getEnvironment().getPropertySources();
    System.out.println("jdbcUrl=="+sources.iterator().next().getProperty("jdbcUrl"));
}

我得到了jdbc网址成功。怎么了?

最佳答案

如果您使用 Apache Maven,则需要将属性文件放置在 src/test/resources 中。这应该会获取您的属性。

关于java - Spring JUnit .properties 文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37086010/

相关文章:

java - 在 OOP 中使用太多单例和静态方法会有什么风险?

Java REST boolean 字段的部分更新

java - Spring 框架 application.properties 与 logback.xml

java - Spring /hibernate : InvocationTargetException when deleting entity

java - Spring MVC 多选列表框值无法返回 Controller

java - 尝试使用 Springs RestTemplate 执行 POST 请求时获取 RestClientException

java - 如何将 JPA 与随机数据库(即用户提交的)一起使用

java - Hibernate - NClob 属性

java - Android 代码调用或调用包含字母 *123*abc# 而不是数字 (*123#) 的字符串?

java - Spring Data Elasticsearch (4.x) - 在 _source 中使用 @Id 强制 id 字段