How to redirect redirected layout requests in Spring MVC REST test?

In my web.xml, I rendered the servlet in / api as follows:

<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>

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

But in the test, when I call mockMvc.perform(get("/api/todo/")), I got 404. I need to call get("/todo/")to get the correct match in the test. I hope to display the controller in the test, as in a real scenario. Is it possible?

I use WebMvcConfigurerAdapterin the test and I tried

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

but it didn’t work.

+1
source share
1 answer

, MockMvc , web.xml. , MockMvc DispatcherServlet. MVC, @Controller beans ..

, MockMvc TestDispatcherServlet, -.

, url-pattern MockMvc. , .

, .

. - HTTP- , .

unit-testing. - , .


,

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

. , - webapp. url-pattern .

+1

Source: https://habr.com/ru/post/1673110/


All Articles