2013-03-03 44 views
0

我在这里很新,我不想诉诸问题,但似乎我别无选择。Spring with Maven:Maven没有找到我的资源

我正在写自动化测试,前一段时间我发现了Spring和Dependancy Injection及其所有优点。然后我用它在我的测试作为我的主要数据提供者(虽然我的一个同事用这种用法不同意,我不明白为什么),像这样:

/src/test/java/automation/data上市:

@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风格。

如果有人有线索......

+2

你可以在'target/test-classes/spring'下找到这个文件吗?你的pom是怎样的? – 2013-03-03 17:59:59

+0

好吧,我看到在那里的xml,但它仍然无法正常工作 – jereshi 2013-03-04 08:18:43

+0

我有同样的问题,并发现了一个类似的问题/答案,解决我的问题在这里:http://stackoverflow.com/questions/3571149/test-resources-的-依赖条件 - 非氟类路径 – kfox 2013-10-24 19:13:17

回答

2

这是对我们有用的东西。让我知道如果你找到另一种方式。

<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> 
1

我使用spring test classes和junit。确保你把正确的依赖关系在你的pom.xml:

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-test</artifactId> 
    <version>3.2.1.RELEASE</version> 
</dependency> 

我注释测试用:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath:test-context.xml") 

而且我测试context.xml的是:源/测试/资源/ test-context.xml

就是这样!有了这个配置的作品!