【问题标题】:Custom view resolver missing in Spring testSpring 测试中缺少自定义视图解析器
【发布时间】:2014-10-14 11:24:18
【问题描述】:

我正在对我的 Spring Web 应用程序进行集成测试,并且需要验证生成的 HTML 页面。 我正在使用完整的 Web 应用程序上下文进行测试,但自定义视图解析器 (JTwig) 存在问题。这是我的代码:

测试类:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = {Config.class})
    @WebAppConfiguration
    public class FrontControllerIntegrationTest {
    private MockMvc mockMvc;

    @Autowired
    WebApplicationContext wac;

    @InjectMocks
    private FrontController frontController = new FrontController();

    @Before
    public void setup() throws JAXBException, XMLStreamException {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void mainSearchPage() throws Exception {
        mockMvc.perform(get("/search/ww/en/altivar"))
               .andExpect(status().isOk())
               .andExpect(view().name("searchResults"))
               .andExpect(model().attribute("idolResponse", isA(SearchResults.class)))
               .andExpect(model().attribute("numberOfPages", is(188)))
               .andExpect(model().attribute("stateId", isNull()));
    }

}

自定义视图解析器:

public class Config extends WebMvcConfigurerAdapter implements WebApplicationInitializer {
...
@Bean
public ViewResolver viewResolver() {
    JtwigViewResolver view = new JtwigViewResolver();
    view.setPrefix("/WEB-INF/templates/");
    view.setSuffix(".twig");
    return view;
}

测试失败并出现错误:

Caused by: com.lyncode.jtwig.exception.ResourceException: Resource /WEB-INF/templates/searchResults.twig not found
    at com.lyncode.jtwig.resource.WebJtwigResource.retrieve(WebJtwigResource.java:36)
    at com.lyncode.jtwig.parser.parboiled.JtwigContentParser.parse(JtwigContentParser.java:62)
    ... 54 more

经过更多挖掘,我知道异常是在哪里引发的:

@Override
public InputStream retrieve() throws ResourceException {
    InputStream resourceAsStream = servletContext.getResourceAsStream(url);
    if (resourceAsStream == null) throw new ResourceException("Resource "+url+" not found");
    return resourceAsStream;
}

servletContextorg.springframework.mock.web.MockServletContext

【问题讨论】:

    标签: java spring unit-testing spring-mvc integration-testing


    【解决方案1】:

    查看此示例:https://github.com/lyncode/jtwig-examples/blob/master/simple-webapp/src/test/java/com/lyncode/jtwig/example/ControllerTest.java

    注意注解@WebAppConfiguration,默认情况下,假定执行目录是你的项目的目录,并且它遵循默认的maven结构。

    如果您在某些 IDE 上运行测试,它可能不符合此要求。另外,如果你的项目不遵循默认的 maven 结构,你需要在注解中传递 webapp 目录位置。

    查看文档:http://docs.spring.io/spring-framework/docs/3.2.0.BUILD-SNAPSHOT/api/org/springframework/test/context/web/WebAppConfiguration.html

    【讨论】:

      【解决方案2】:

      您的资源可能没有从“主”目录复制到“测试”。尝试运行测试并转到 $PROJECT_HOME/target/$PROJECT_NAME - 那里是否有带有模板的 WEB_INF 文件夹(如果它的 maven 项目,我不确定 gradle 是否使用相同的结构)?

      如果它们不存在,那么您可能必须在 pom.xml 中定义节点。看一下文档: http://maven.apache.org/plugins/maven-resources-plugin/testResources-mojo.html

      【讨论】:

      • 我用 尝试了不同的目录,但没有成功。我可以确认在构建工件时模板已复制到目标目录,但在清理目标并运行搜索后它仍然是空的,就像测试不会使用它一样。
      猜你喜欢
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多