jereshi
jereshi

Reputation: 1

Spring with Maven: Maven does not find my resources

I'm pretty new here and I didn't want to resort to ask but it seems I got no choice.

I'm writing automation tests and some time ago I discovered Spring and the Dependancy Injection and all its advantages. I've then used it in my tests as my primary data provider (though one of my colleagues disagrees with this usage, I don't understand why), like this:

/src/test/java/automation/data listing:

@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();
        }
    }
}

and of course I've got my XMLs inside /src/test/resources/spring/.

The funny thing is that this works fine if I run it with IntelliJ, but maven refuses to run, giving me that damn error:

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

I've looked around all the internet for hours, trying all kinds of things:

... no dice.

So I'm kinda stuck here, and I'm seriously thinking of reverting back to old Java style with the hardcoded parameters.

If someone has a clue...

Upvotes: 0

Views: 1977

Answers (2)

Gerard Ribas
Gerard Ribas

Reputation: 727

I work with spring test classes and junit. Be sure that you put the correct dependencies in your pom.xml:

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

I annotate the tests with:

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

And my test-context.xml was in: src/test/resources/test-context.xml

That's it! With this config works!

Upvotes: 1

Adisesha
Adisesha

Reputation: 5258

This is something worked for us. Let me know if you find alternative way.

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

Upvotes: 2

Related Questions