Home  >  Article  >  Java  >  Code that simulates HTTP requests and calls the controller

Code that simulates HTTP requests and calls the controller

不言
不言forward
2019-03-14 11:04:333214browse

This article brings you the code about simulating HTTP requests to call the controller. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Written in front

MockMvc implements the simulation of Http requests. It can directly use the network form and convert to Controller calls, which makes the test speed faster. Fast and does not depend on the network environment. It also provides a set of verification tools.

The single test code is as follows:

@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
public class MyControllerTest {
  @Autowired
  private MockMvc mockMvc;
  /**
   * 测试方法
   */
  private void bindAndUnbindTenantPoiTest() throws Exception {
    MvcResult mvcResult = mockMvc.perform(post(${"访问的url"})
        .param("${key1}", "${value1}")
        .param("${key2}", "${value2}")
        .param("${key3}", "${value3}")) 
        .andDo(print()) // 定义执行行为
        .andExpect(status().isOk()) // 对请求结果进行验证
        .andReturn(); // 返回一个MvcResult
    jsonObject = toJsonObject(mvcResult);
    assert jsonObject.getIntValue("code") == code; // 断言返回内容是否符合预期
    assert message.equals(jsonObject.getString("message"));
  }  
}

Perform introduction

perform is used to call controller business logic. There are many methods such as post and get. For details, please refer to Using Junit MockMvc Mockito Unit test Http requests

Parameter Param introduction

Add http request parameters through param, the format is K-V, add one parameter one parameter or add MultiValueMap through params. Part of the source code of parma is as follows:

/**
     * Add a request parameter to the {@link MockHttpServletRequest}.
     * <p>If called more than once, new values get added to existing ones.
     * @param name the parameter name
     * @param values one or more values
     */
    public MockHttpServletRequestBuilder param(String name, String... values) {
        addToMultiValueMap(this.parameters, name, values);
        return this;
    }

    /**
     * Add a map of request parameters to the {@link MockHttpServletRequest},
     * for example when testing a form submission.
     * <p>If called more than once, new values get added to existing ones.
     * @param params the parameters to add
     * @since 4.2.4
     */
    public MockHttpServletRequestBuilder params(MultiValueMap<String, String> params) {
        for (String name : params.keySet()) {
            for (String value : params.get(name)) {
                this.parameters.add(name, value);
            }
        }
        return this;
    }

Written at the back

Another pitfall is when using annotations, check whether there is overlap between annotations, otherwise an error will be reported. It is wrong to use @WebMvcTest @Configuration at the same time. For details, you can view the annotation source code

The above is the detailed content of Code that simulates HTTP requests and calls the controller. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete