如何为独立的 MockMvc 启用控制器参数验证 [英] How to enable controller parameter validation for standalone MockMvc

查看:32
本文介绍了如何为独立的 MockMvc 启用控制器参数验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控制器

@RestController
@Validated
class MyController {

    @GetMapping("/foo")
    public String unwrapped(@Min(1) @RequestParam("param") int param) {
        return Integer.toString(param);
    }

    @GetMapping("/bar")
    public String wrapped(@ModelAttribute @Valid Wrapper bean) {
        return Integer.toString(bean.param);
    }

    static class Wrapper {

        @Min(1)
        int param;

        public void setParam(final int param) {
            this.param = param;
        }
    }
}

测试

public class MyControllerTest {

    MyController controller = new MyController();
    MockMvc mockMvc = MockMvcBuilders
            .standaloneSetup(this.controller)
            .build();

    @Test // fails
    public void unwrapped() throws Exception {
        this.mockMvc.perform(get("/foo")
                .param("param", "0"))
                .andExpect(status().isBadRequest());
    }

    @Test // passes
    public void wrapped() throws Exception {
        this.mockMvc.perform(get("/bar")
                .param("param", "0"))
                .andExpect(status().isBadRequest());
    }
}

要在 spring 中启用(解包)方法参数验证,必须使用 @Validated 注释控制器,并且必须将 MethodValidationPostProcessor 添加到上下文中.
是否可以将 MethodValidationPostProcessor bean 添加到独立设置中?
问题可能会简化为如何将 BeanPostProcessor 添加到独立的 MockMvc 设置中?

To enable (unwrapped) method parameter validation in spring the controller has to be annotated with @Validated and the MethodValidationPostProcessor must be added to the context.
Is it possible to add the MethodValidationPostProcessor bean to the standalone setup ?
The question might be reduced to how to add a BeanPostProcessor to a standalone MockMvc setup ?

推荐答案

是否可以将 MethodValidationPostProcessor bean 添加到独立设置中?

Is it possible to add the MethodValidationPostProcessor bean to the standalone setup ?

不,不幸的是,在使用 MockMvcBuilders.standaloneSetup() 时这是不可能的,因为这会在幕后创建一个 StubWebApplicationContext支持BeanPostProcessors.

No, that is unfortunately not possible when using MockMvcBuilders.standaloneSetup() since that creates a StubWebApplicationContext behind the scenes which does not support BeanPostProcessors.

一旦 SPR-6380 得到解决(目前在 Spring Framework 5.x 中).x backlog),您将不再需要依赖 MethodValidationPostProcessor.但是,在此期间,您需要切换到使用 MockMvcBuilders.webAppContextSetup() 以便能够测试通过 MethodValidationPostProcessor 添加到您的控制器的行为.

Once SPR-6380 has been addressed (currently in the Spring Framework 5.x backlog), you will no longer need to rely on the MethodValidationPostProcessor. However, in the interim you will need to switch to the use of MockMvcBuilders.webAppContextSetup() in order to be able to test the behavior added to you controller via MethodValidationPostProcessor.

我认为不可能直接做到这一点;但是,您可以创建 StandaloneMockMvcBuilder 的自定义子类,该子类覆盖 initWebAppContext() 方法,委托给 super.initWebAppContext() 以创建 WebApplicationContext,然后通过org.springframework.test.web.servlet.setup.StubWebApplicationContext.addBean(String, Object)以编程方式注册MethodValidationPostProcessor bean.

I don't think it's possible to do that directly; however, you could create a custom subclass of StandaloneMockMvcBuilder that overrides the initWebAppContext() method, delegates to super.initWebAppContext() to create the WebApplicationContext, and then registers the MethodValidationPostProcessor bean programmatically via org.springframework.test.web.servlet.setup.StubWebApplicationContext.addBean(String, Object).

这篇关于如何为独立的 MockMvc 启用控制器参数验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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