我是新来的,我不想询问,但似乎我别无选择。
我正在编写自动化测试,不久前我发现了 Spring 和依赖注入(inject)及其所有优点。然后我在测试中使用它作为我的主要数据提供者(尽管我的一位同事不同意这种用法,我不明白为什么),如下所示:
/src/test/java/automation/data
list :
@ContextConfiguration("classpath:/spring/surveyFlows.xml")
public class SurveyDataProvider
{
@Autowired
private List<Survey> allSurveys;
public List<Survey> getAllSurveys()
{
return allSurveys;
}
public SurveyDataProvider()
{
TestContextManager testContextManager = new TestContextManager(getClass());
try
{
testContextManager.prepareTestInstance(this);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
当然,我的 XML 已经在里面了
/src/test/resources/spring/
。
有趣的是,如果我用 IntelliJ 运行它,它就可以正常工作,但是 maven
拒绝运行,给我那个该死的错误:
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/surveyFlows.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/surveyFlows.xml] cannot be opened because it does not exist
我在互联网上浏览了几个小时,尝试了各种各样的事情:
- 将资源移动到
/src/main
- 将资源目录放入
pom.xml
...没有骰子。
所以我有点被困在这里,我正在认真考虑使用硬编码参数恢复到旧的 Java 风格。
如果有人有线索......
最佳答案
这对我们有用。如果您找到替代方法,请告诉我。
<build>
<!--Your other configuration -->
<testResources>
<!-- Not sure if including resource from main is needed -->
<testResource>
<directory>${project.basedir}/src/main/resources</directory>
</testResource>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
</build>
关于java - Spring 与 Maven : Maven does not find my resources,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15188734/