package cn.test.demo.base_demo.entity.po; import java.util.Date; /** * @author 王杨帅 * @create 2018-05-05 22:03 * @desc 用户实体类 **/ public class User { private Integer userId; private String userName; private String password; private Date birthday; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
package cn.test.demo.base_demo.controller; import cn.test.demo.base_demo.entity.po.User; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; /** * @author 王杨帅 * @create 2018-05-05 22:02 * @desc 用户模块控制层 **/ @RestController @RequestMapping(value = "/user") public class UserController { @GetMapping public List<User> queryList() { List<User> userList = new ArrayList<>(); userList.add(new User()); userList.add(new User()); userList.add(new User()); return userList; } }
该测试类主要测试用户控制类中的相关方法
@RunWith(SpringRunner.class) @SpringBootTest @Slf4j
@Autowired private WebApplicationContext wac;
private MockMvc mockMvc; @Before public void setup() { mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); }
@Test public void queryList() throws Exception { String result = mockMvc.perform( MockMvcRequestBuilders.get("/user") .contentType(MediaType.APPLICATION_JSON_UTF8) ) .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn().getResponse().getContentAsString(); log.info("===/" + className + "/queryList===" + result); }
package cn.test.demo.base_demo.controller; import lombok.extern.slf4j.Slf4j; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvcBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import static org.junit.Assert.*; @RunWith(SpringRunner.class) @SpringBootTest @Slf4j public class UserControllerTest { private final String className = getClass().getName(); @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } @Test public void queryList() throws Exception { String result = mockMvc.perform( MockMvcRequestBuilders.get("/user") .contentType(MediaType.APPLICATION_JSON_UTF8) ) .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn().getResponse().getContentAsString(); log.info("===/" + className + "/queryList===" + result); } }
未完待续
原文:https://www.cnblogs.com/NeverCtrl-C/p/8996564.html