【问题标题】:Spring JUnit .properties file does not workSpring JUnit .properties 文件不起作用
【发布时间】:2016-05-07 07:55:58
【问题描述】:

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

@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,dataSource配置在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 网址成功。怎么了?

【问题讨论】:

    标签: java spring jpa jdbc junit


    【解决方案1】:

    如果您使用的是 Apache Maven,您需要将属性文件放在 src/test/resources 中。那应该会拿起你的财产。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-15
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      • 2012-05-29
      • 1970-01-01
      • 2018-12-01
      相关资源
      最近更新 更多