0

我在这里很新,我不想问,但似乎我别无选择。

我正在编写自动化测试,前段时间我发现了 Spring 和依赖注入及其所有优点。然后我在测试中将它用作我的主要数据提供者(尽管我的一位同事不同意这种用法,但我不明白为什么),如下所示:

/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拒绝运行,给我那个该死的错误:

引起:org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/surveyFlows.xml]; 嵌套异常是 java.io.FileNotFoundException: 类路径资源 [spring/surveyFlows.xml] 无法打开,因为它不存在

我已经浏览了所有互联网几个小时,尝试了各种事情:

  • 将资源转移到/src/main
  • 将资源目录放在pom.xml

... 没有骰子。

所以我有点卡在这里,我正在认真考虑使用硬编码参数恢复到旧的 Java 风格。

如果有人有线索...

4

2 回答 2

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>
于 2013-03-03T18:03:16.200 回答
1

我使用弹簧测试类和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")

而我的 test-context.xml 位于:src/test/resources/test-context.xml

而已!有了这个配置就可以了!

于 2013-03-03T19:08:25.177 回答