<dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> </dependency>
package com.xiaofangtech.sunt.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;
@Entity
@Table(name="t_userinfo")
public class UserInfo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(min=0, max=32)
private String name;
private Integer age;
@Size(min=0, max=255)
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.xiaofangtech.sunt.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.xiaofangtech.sunt.bean.UserInfo;
import com.xiaofangtech.sunt.repository.UserInfoRepository;
import com.xiaofangtech.sunt.utils.*;
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserInfoRepository userRepositoy;
/***
* 根据用户id,获取用户信息
* @param id
* @return
*/
@RequestMapping(value="getuser", method=RequestMethod.GET)
public Object getUser(Long id)
{
UserInfo userEntity = userRepositoy.findOne(id);
ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), userEntity);
return resultMsg;
}
/***
* 获取所有用户列表
* @return
*/
@RequestMapping(value="getalluser", method=RequestMethod.GET)
public Object getUserList()
{
List<UserInfo> userEntities = (List<UserInfo>) userRepositoy.findAll();
ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), userEntities);
return resultMsg;
}
/***
* 新增用户信息
* @param userEntity
* @return
*/
@Modifying
@RequestMapping(value="adduser", method=RequestMethod.POST)
public Object addUser(@RequestBody UserInfo userEntity)
{
userRepositoy.save(userEntity);
ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), userEntity);
return resultMsg;
}
/***
* 更新用户信息
* @param userEntity
* @return
*/
@Modifying
@RequestMapping(value="updateuser", method=RequestMethod.PUT)
public Object updateUser(@RequestBody UserInfo userEntity)
{
UserInfo user = userRepositoy.findOne(userEntity.getId());
if (user != null)
{
user.setName(userEntity.getName());
user.setAge(userEntity.getAge());
user.setAddress(userEntity.getAddress());
userRepositoy.save(user);
}
ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), user);
return resultMsg;
}
/***
* 删除用户
* @param id
* @return
*/
@Modifying
@RequestMapping(value="deleteuser", method=RequestMethod.DELETE)
public Object deleteUser(Long id)
{
try
{
userRepositoy.delete(id);
}
catch(Exception exception)
{
}
ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), null);
return resultMsg;
}
}
package com.xiaofangtech.sunt;
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.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
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 org.springframework.web.context.WebApplicationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaofangtech.sunt.bean.UserInfo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.hamcrest.Matchers.*;
//这是JUnit的注解,通过这个注解让SpringJUnit4ClassRunner这个类提供Spring测试上下文。
@RunWith(SpringJUnit4ClassRunner.class)
//这是Spring Boot注解,为了进行集成测试,需要通过这个注解加载和配置Spring应用上下
@SpringApplicationConfiguration(classes = SpringJUnitTestApplication.class)
@WebAppConfiguration
public class SpringJUnitTestApplicationTests {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setupMockMvc() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
/***
* 测试添加用户接口
* @throws Exception
*/
@Test
public void testAddUser() throws Exception
{
//构造添加的用户信息
UserInfo userInfo = new UserInfo();
userInfo.setName("testuser2");
userInfo.setAge(29);
userInfo.setAddress("北京");
ObjectMapper mapper = new ObjectMapper();
//调用接口,传入添加的用户参数
mockMvc.perform(post("/user/adduser")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(mapper.writeValueAsString(userInfo)))
//判断返回值,是否达到预期,测试示例中的返回值的结构如下{"errcode":0,"errmsg":"OK","p2pdata":null}
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
//使用jsonPath解析返回值,判断具体的内容
.andExpect(jsonPath("$.errcode", is(0)))
.andExpect(jsonPath("$.p2pdata", notNullValue()))
.andExpect(jsonPath("$.p2pdata.id", not(0)))
.andExpect(jsonPath("$.p2pdata.name", is("testuser2")));
}
/***
* 测试更新用户信息接口
* @throws Exception
*/
@Test
public void testUpdateUser() throws Exception
{
//构造添加的用户信息,更新id为2的用户的用户信息
UserInfo userInfo = new UserInfo();
userInfo.setId((long)2);
userInfo.setName("testuser");
userInfo.setAge(26);
userInfo.setAddress("南京");
ObjectMapper mapper = new ObjectMapper();
mockMvc.perform(put("/user/updateuser")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(mapper.writeValueAsString(userInfo)))
//判断返回值,是否达到预期,测试示例中的返回值的结构如下
//{"errcode":0,"errmsg":"OK","p2pdata":null}
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.errcode", is(0)))
.andExpect(jsonPath("$.p2pdata", notNullValue()))
.andExpect(jsonPath("$.p2pdata.id", is(2)))
.andExpect(jsonPath("$.p2pdata.name", is("testuser")))
.andExpect(jsonPath("$.p2pdata.age", is(26)))
.andExpect(jsonPath("$.p2pdata.address", is("南京")));
}
/***
* 测试根据用户id获取用户信息接口
* @throws Exception
*/
@Test
public void testGetUser() throws Exception
{
mockMvc.perform(get("/user/getuser?id=2"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.errcode", is(0)))
.andExpect(jsonPath("$.p2pdata", notNullValue()))
.andExpect(jsonPath("$.p2pdata.id", is(2)))
.andExpect(jsonPath("$.p2pdata.name", is("testuser")))
.andExpect(jsonPath("$.p2pdata.age", is(26)))
.andExpect(jsonPath("$.p2pdata.address", is("南京")));
}
/***
* 测试获取用户列表接口
* @throws Exception
*/
@Test
public void testGetUsers() throws Exception
{
mockMvc.perform(get("/user/getalluser"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.errcode", is(0)))
.andExpect(jsonPath("$.p2pdata", notNullValue()));
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有