mockmvc standalonesetup如何注入freemarker viewresolver?

时间:2014-12-02 04:10:38

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

我使用春季3.2.0中的弹簧测试框架来测试我的弹簧mvc控制器。

我试图创建一个独立的测试设置,这样我就可以完全控制所有变量。 我已经使用InternalResourceViewResolver进行了测试。 但现在我试图使用freemarker作为我的模板引擎,我无法弄清楚如何将freemarker配置作为bean包含在独立设置中。

以下课程:

@RunWith(MockitoJUnitRunner.class)
public class StandAloneRegistrationControllerTest {

private MockMvc mockMvc;

@Mock
private UserRegistrationService userRegistrationServiceMock;

@Before
public void setUp() {
    mockMvc = MockMvcBuilders.standaloneSetup(new RegistrationController())
            .setViewResolvers(viewResolver())
            .build();
}

private ViewResolver viewResolver() {
    FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();

    freeMarkerViewResolver.setCache(false);
    freeMarkerViewResolver.setPrefix("/view/");
    freeMarkerViewResolver.setSuffix(".ftl");
    freeMarkerViewResolver.setContentType("text/html;charset=UTF-8");
    freeMarkerViewResolver.setRequestContextAttribute("rc");
    freeMarkerViewResolver.setExposeSpringMacroHelpers(true);
    freeMarkerViewResolver.setExposeRequestAttributes(true);
    freeMarkerViewResolver.setExposeSessionAttributes(true);
    freeMarkerViewResolver.setAllowSessionOverride(true);


    return freeMarkerViewResolver;
}

@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setTemplateLoaderPath("/WEB-INF/");
    configurer.setDefaultEncoding("UTF-8");
    Properties settings = new Properties();
    settings.put("auto_import", "/view/layouts/layouts.ftl as layout,spring.ftl as spring");
    configurer.setFreemarkerSettings(settings);

    return configurer;
}

@Test
public void showAddTodoForm_ShouldCreateFormObjectAndRenderAddTodoForm() throws Exception {
    mockMvc.perform(get("/register"))
            .andExpect(status().isOk());

}

}

给出以下例外:

org.springframework.web.util.NestedServletException: Request processing failed; 
nested exception is org.springframework.context.ApplicationContextException: 
Must define a single FreeMarkerConfig bean in this web application context (may be inherited): 
FreeMarkerConfigurer is the usual implementation. This bean may be given any name.; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [org.springframework.web.servlet.view.freemarker.FreeMarkerConfig] is defined

如何将freemarker配置bean添加到上下文中,以便我可以将freemarker视图解析器注入mockmvc独立设置?

1 个答案:

答案 0 :(得分:0)

我不熟悉MockMVC,但您是否尝试使用FreeMarker Configuration而不是setFreemarkerSettings

示例:

@Bean
public FreeMarkerConfigurer freeMarkerConfigurer(WebApplicationContext applicationContext) throws IOException, TemplateException {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();

    freemarker.template.Configuration configuration = configurer.createConfiguration();
    configuration.addAutoInclude("/templates/include-common.ftl");
    configuration.setWhitespaceStripping(true);

    configuration.setDefaultEncoding("UTF-8");
    configuration.setOutputEncoding("UTF-8");
    configuration.setURLEscapingCharset("UTF-8");
    configuration.setServletContextForTemplateLoading(applicationContext.getServletContext(), "/WEB-INF/ftl/");

    configurer.setConfiguration(configuration);

    return configurer;
}