12

I am trying to unit test a webapp that I am developing in Maven/Eclipse. It is a Spring MVC webapp, and I need some unit tests to test my model controller.

To do this, I need to use my webapp config .xml files to inject the controller, however, all my configuration (and other associated files that the configs reference) are in the src/main/webapp directory (as per Maven convention) - there are a lot of files in here that i need to reference in my tests, but they are not available when running the test as they are not on the classpath (Maven only seems to be adding src/main/java, src/test/java, src/main/resources, src/test/resources to the classpath but not src/main/webapps)

I do not want to have to copy my entire webapp in to my src/test/resources folder just for the purpose of testing - is there any other way to be able to access them?

I can get around this by changing the Eclipse "Run Configurations" to include this folder on the classpath, but that will not work when running the tests from the command line?

Has anyone else encountered this? is there a know solution?

Thanks!

4 Answers 4

6

In general, you should be using mocks (e.g., EasyMock or jMock) to handle unit testing for your controller; a unit test should test a component in isolation from the rest of the system. In Spring 3, you can define your Controllers as a POJO with annotations and then simply test the Controllers without any interaction from Spring at all.

If you wish to do more integration-level testing, then you can place common parts of config files in src/main/resources so they become accessible in the classpath. Then use a test specific configuration in src/test/resources for your test. That file can either import those files along with anything you need, or you can annotate your test case with @ContextConfiguration to specify the multiple files that are needed to assemble the context.

For a concrete example, see this blog post on integration testing Spring MVC. The Testing chapter of the Spring manual also has a decent overview of integration testing in Spring.

5

You can achieve this using maven-surefire-plugin as below:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <additionalClasspathElements>
                <additionalClasspathElement>src\main\webapp\</additionalClasspathElement>
            </additionalClasspathElements>
        </configuration>
    </plugin>

Now you can access your web.xml in this way for example:

getClass().getClassLoader().getResourceAsStream("classpath:webapp/WEB-INF/web.xml")
3
  • It worked for me using @ContextConfiguration(locations = { "classpath:/WEB-INF/beans.xml" })
    – Ena
    Feb 28, 2013 at 17:09
  • Mark, Firstly thanks for the idea. But your suggestion is very hard-coded. 1. Windows-based file separator is used. 2. Relative path is used. If you run tests from parent Maven module it will add your path (src\main\webapp) to its (parent module) path and you'll get illegal path to the resource as a result. I would change it to "<additionalClasspathElement>${project.basedir}${file.separator}src${file.separator}main${file.separator}webapp</additionalClasspathElement>" Jun 16, 2015 at 12:32
  • Also there is more elegant way to access the resource. Try the following "getClass().getClassLoader().getResourceAsStream("/WEB-INF/web.xml")". You can replace root dir with OS independent one using File#listRoots method Jun 16, 2015 at 12:33
4

Emil's first link was correct in showing the correct @ContextConfiguration location(s).

If your Spring configuration files are in src/main/webapp/**, then they will not be on the classpath when built by Maven. To get the configuration for integration testing, simple specify the path using the file: prefix.

@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/app-config.xml")

If using Spring's STS plugin to create a new Spring MVC project, you will notice that the application context config files are put in the location of my example. The way to use them in integration testing is then to specify the path using the file: prefix as described above.

0

To do this, I need to use my webapp config .xml files to inject the controller, however, all my configuration (and other associated files that the configs reference) are in the src/main/webapp directory

What are those files that you also need during unit tests exactly?

I do not want to have to copy my entire webapp in to my src/test/resources folder just for the purpose of testing - is there any other way to be able to access them?

No. If you want them to be on the classpath, put them either in src/main/resources or src/test/resources, files under src/main/webapp are not on the classpath because they're not supposed to.

2
  • Thanks for the quick reply, The files I need are spring webmvc-config files, as i need to create a mock context for my web controller - and unfortunately the config files also contain reference to other files within the webapps folder. Fortunately I have now got around this by creating "test" spring config files that do not refer to any of the additional webapp resources that are not of relevance (just the controller beans required to unit test) and have put just my custom test config files in the src/test/resources.
    – rhinds
    Apr 23, 2010 at 14:45
  • I have src/test/resources but still my junit looking for src/main/webapp
    – Bilgehan
    Jan 17, 2020 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.