Johnny Lim
Johnny Lim

Reputation: 5843

In Spring + Tiles 2, how to unit-test controllers?

When I was trying with the following code:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({ "classpath:applicationContext.xml",
        "file:WebContent/WEB-INF/spring/appServlet/servlet-context.xml" })
@ActiveProfiles("unittest")
public class PlaygroundControllerTest {

    @Test
    public void test() {

    }

}

I encountered the following exception:

Caused by: java.io.FileNotFoundException: ServletContext resource [/WEB-INF/tiles/tiles.xml] cannot be resolved to URL because it does not exist
    at org.springframework.web.context.support.ServletContextResource.getURL(ServletContextResource.java:154)
    at org.springframework.web.servlet.view.tiles2.SpringTilesApplicationContextFactory$SpringWildcardServletTilesApplicationContext.getResources(SpringTilesApplicationContextFactory.java:105)
    at org.springframework.web.servlet.view.tiles2.TilesConfigurer$SpringTilesContainerFactory.getSourceURLs(TilesConfigurer.java:423)
    ... 49 more

This is the related tiles configuration in servlet-context.xml.

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles/tiles.xml</value>
        </list>
    </property>
</bean>

How can I make the unit-test find the configuration file of the tiles (/WEB-INF/tiles/tiles.xml)?

Upvotes: 2

Views: 969

Answers (1)

Vlad Bochenin
Vlad Bochenin

Reputation: 3082

I encountered the same problem...

By default, Spring use "src/main/webapp" path. You can change path on "you_ application/src/main/webapp".

For example,

@WebAppConfiguration("webapp/src/main/webapp")

Upvotes: 1

Related Questions