这是我的测试类中的注释配置。
@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/