隔离控制器测试无法实例化Pageable [英] Isolated Controller Test can't instantiate Pageable

查看:90
本文介绍了隔离控制器测试无法实例化Pageable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring MVC控制器,该控制器使用Spring-Data的分页支持:

I have a Spring MVC Controller which uses Pagination Support of Spring-Data:

@Controller
public class ModelController {

    private static final int DEFAULT_PAGE_SIZE = 50;

    @RequestMapping(value = "/models", method = RequestMethod.GET)
    public Page<Model> showModels(@PageableDefault(size = DEFAULT_PAGE_SIZE) Pageable pageable, @RequestParam(
            required = false) String modelKey) {

//..
        return models;
    }

}

我想使用不错的Spring MVC测试支持来测试RequestMapping.为了使这些测试快速进行并与其他所有事情隔离开,我不想创建完整的ApplicationContext:

And I'd like to test the RequestMapping using the nice Spring MVC Test Support. In order to keep these tests fast and isolated from all the other stuff going on, I do not want to create the complete ApplicationContext:

public class ModelControllerWebTest {
    private MockMvc mockMvc;

    @Before
    public void setup() {
        ModelController controller = new ModelController();
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
    }

    @Test
    public void reactsOnGetRequest() throws Exception {
        mockMvc.perform(get("/models")).andExpect(status().isOk());
    }

}

这种方法在其他不希望使用Pageable的Controller上也能很好地工作,但是有了这个,我得到了这些不错的Spring长堆栈跟踪之一.它抱怨无法实例化Pageable:

This approach works fine with other Controllers, that do not expect a Pageable, but with this one I get one of these nice long Spring stacktraces. It complains about not being able to instantiate Pageable:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.domain.Pageable]: Specified class is an interface
at   
.... lots more lines

问题:如何更改测试,以便魔术般地进行无请求参数到可分页"转换?

Question: How do I change my test so the magic No-Request-Parameter-To-Pageable conversion happens properly?

注意:在实际的应用程序中,一切正常.

Note: In the actual application everything is working fine.

推荐答案

可分页的问题可以通过提供自定义参数处理程序来解决.如果设置了此选项,则将在ViewResolver异常(循环)中运行.为了避免这种情况,您必须设置一个ViewResolver(例如,一个匿名JSON ViewResolver类).

The problem with pageable can be solved by providing a custom argument handler. If this is set you will run in a ViewResolver Exception (loop). To avoid this you have to set a ViewResolver (an anonymous JSON ViewResolver class for example).

mockMvc = MockMvcBuilders.standaloneSetup(controller)
            .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
            .setViewResolvers(new ViewResolver() {
                @Override
                public View resolveViewName(String viewName, Locale locale) throws Exception {
                    return new MappingJackson2JsonView();
                }
            })
            .build();

这篇关于隔离控制器测试无法实例化Pageable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆