2013-07-16 42 views
2

當我用下面的代碼嘗試:在Spring + Tiles 2中,如何對控制器進行單元測試?

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() { 

    } 

} 

我遇到以下異常:

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 

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

如何使單元測試找到瓷磚的配置文件(/WEB-INF/tiles/tiles.xml)?

回答

1

我遇到同樣的問題...

默認情況下,春用 「的src/main/webapp的」 路徑。您可以在「you_application/src/main/webapp」上更改路徑。

例如,

@WebAppConfiguration("webapp/src/main/webapp") 
相關問題