如何在Spring MVC REST测试中重定向重定向模拟请求? [英] How to redirect redirect mock requests in Spring MVC REST test?

查看:86
本文介绍了如何在Spring MVC REST测试中重定向重定向模拟请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的web.xml中,这样将servlet映射到/api:

In my web.xml I mapped the servlet to /api like this:

<servlet-mapping>
    <servlet-name>todo</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>todo</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

因此对于使用@RequestMapping(value = "/todo", method = RequestMethod.GET)的控制器,它实际上已映射到"/api/todo"

So for the controller using @RequestMapping(value = "/todo", method = RequestMethod.GET), it actually gets mapped to "/api/todo"

但是在测试中,当我调用mockMvc.perform(get("/api/todo/"))时,我得到了404.我必须调用get("/todo/")才能在测试中获得正确的映射.我希望像实际情况一样在测试中映射控制器.有可能吗?

But in test when I call mockMvc.perform(get("/api/todo/")) I got a 404. I have to call get("/todo/") to get the correct mapping in test. I hope to map the controller in test like in actual scenario. Is that possible?

我在测试中使用了WebMvcConfigurerAdapter并且尝试了

I'm using WebMvcConfigurerAdapter in test and I tried

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/api/**").addResourceLocations("/");
}

但是没有用.

推荐答案

在设置MockMvc配置时,您会注意到您未指定部署描述符web.xml的位置.这是因为MockMvc不会测试您对DispatcherServlet的配置.它会测试您的MVC配置,@Controller bean等.

You will notice when you setup your MockMvc configuration that you don't specify the location of a deployment descriptor, your web.xml. That is because MockMvc doesn't test your configuration of a DispatcherServlet. It tests your MVC configuration, @Controller beans and the like.

如果您查看源代码,则MockMvc创建一个TestDispatcherServlet,该加载并使用您的Web应用程序上下文.

If you look at the source, MockMvc creates a TestDispatcherServlet which loads and uses your web application context.

换句话说,servlet url-pattern路径与MockMvc不相关.您就像没有这样的路径一样发出请求.

In other words, the servlet url-pattern path is irrelevant to MockMvc. You make your requests as if there was no such path.

您声明

我希望像实际情况一样在测试中映射控制器.

I hope to map the controller in test like in actual scenario.

您将需要使用其他测试策略.完全部署您的Web应用程序,并使用HTTP客户端发出请求并验证您获得的回报.

you will need to use a different test strategy. Fully deploy your web application and use an HTTP client to make requests and validate what you get back.

您已标记unit-testing.完整环境中的测试不是单元测试,而是集成和系统测试.

You've tagged unit-testing. Testing in a full environment is not unit testing, it is integration and system testing.

请注意,

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/api/**").addResourceLocations("/");
}

与上述无关.其目的是从Web应用程序中的某些路径提供静态资源.它与servlet的url-pattern或控制器的路径无关.

has nothing to do with the above. Its purpose is to serve static resources from some path in your webapp. It has nothing to do with your servlet's url-pattern or the path to your controllers.

这篇关于如何在Spring MVC REST测试中重定向重定向模拟请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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