Spring MockMvc Test Executes a Request at an External URL

I am trying to execute a POST request at a URL outside the current context, and it seems like Spring cannot understand it.

Test code:

        String content = mvc
            .perform(post("http://some-external-url.com:8080/somepath)
                    .header("Authorization", authorization)
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                    .param("username", username)
                    .param("password", password)
            .andExpect(status().isOk())
            .andReturn().getResponse().getContentAsString();

Works like a charm in the current context. But the remote service cannot be reached at all. It seems that the http://some-external-url.com:8080 "part is being ignored.

Mock Creation Code:

mvc = MockMvcBuilders.webAppContextSetup(context).build();

Is there any way to make it work? Because using the standard Java class HttpUrlConnection is a huge pain.

+4
source share
1 answer

. Spring MVC Test Framework ( , ).

Spring ,

Spring MVC Test Spring MVC API, JUnit, TestNG . API spring -test , , Servlet.

: Spring MVC Test Servlet. , Servlet ( ).

, , REST Assured Spring MVC Test .

+3

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


All Articles