【发布时间】:2018-03-08 04:21:50
【问题描述】:
我有一个 Junit 测试处理控制器类的 POST 的方法。 Junit 运行良好。但我没有在 MockHttpServletRequest 的日志请求正文中看到打印。谁能解释一下原因?
package com.spring.batch.learnings.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.hamcrest.Matchers.*;
import org.junit.Before;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.spring.batch.learnings.EmployeeListController;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration
public class EmployeeListControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
private final String EMPLOYEE_REQUEST = "[{\"lastName\":\"TESTER\",\"firstName\":\"TONY\"},{\"lastName\":\"NEWBIE\",\"firstName\":\"NICK\"},{\"lastName\":\"INTERMEDIATE\",\"firstName\":\"IAN\"}]";
@Configuration
@EnableAutoConfiguration
public static class Config {
@Bean
public EmployeeListController apiController() {
return new EmployeeListController();
}
}
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void testupdateEmployees() throws Exception {
mockMvc.perform(post("/processedEmployeeList")
.contentType(MediaType.APPLICATION_JSON)
.content(EMPLOYEE_REQUEST))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[0].firstName", is("TONY")))
.andExpect(jsonPath("$[0].lastName", is("TESTER")))
.andExpect(jsonPath("$[1].firstName", is("NICK")))
.andExpect(jsonPath("$[1].lastName", is("NEWBIE")))
.andExpect(jsonPath("$[2].firstName", is("IAN")))
.andExpect(jsonPath("$[2].lastName", is("INTERMEDIATE")));
}
}
下面的日志打印没有 MockHttpServletRequest 的请求正文。当我在请求正文中传递 JSON“EMPLOYEE_REQUEST”时
MockHttpServletRequest:
HTTP Method = POST
Request URI = /processedEmployeeList
Parameters = {}
Headers = {Content-Type=[application/json]}
Handler:
Type = com.spring.batch.learnings.EmployeeListController
Method = public org.springframework.http.ResponseEntity<java.util.List<com.spring.batch.learnings.Employee>> com.spring.batch.learnings.EmployeeListController.updateEmployees(java.util.List<com.spring.batch.learnings.Employee>)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = [{"lastName":"TESTER","firstName":"TONY"},{"lastName":"NEWBIE","firstName":"NICK"},{"lastName":"INTERMEDIATE","firstName":"IAN"}]
Forwarded URL = null
Redirected URL = null
Cookies = []
【问题讨论】:
标签: spring spring-mvc spring-boot