package com.junit.tool;
import cn.hutool.json.JSONUtil;
import com.junit.tool.entity.Activity;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
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.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import javax.annotation.Resource;
import java.util.Date;
/**
* 基于MockMvc测试
*/
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class ActivityTest02 {
protected static Logger logger = LoggerFactory.getLogger(ActivityTest02.class) ;
@Resource
private MockMvc mockMvc ;
private Activity activity = null ;
@Before
public void before () throws Exception {
ResultActions resultAction = mockMvc.perform(MockMvcRequestBuilders.get("/activity/{id}",1)) ;
MvcResult mvcResult = resultAction.andReturn() ;
String result = mvcResult.getResponse().getContentAsString();
activity = JSONUtil.toBean(result,Activity.class) ;
}
@Test
public void updateById () throws Exception {
activity.setId(null);
activity.setCreateTime(new Date());
activity.setOrganizer("One商家");
ResultActions resultAction = mockMvc.perform(MockMvcRequestBuilders.post("/activity")
.contentType(MediaType.APPLICATION_JSON)
.content(JSONUtil.toJsonStr(activity))) ;
MvcResult mvcResult = resultAction.andReturn() ;
String result = mvcResult.getResponse().getContentAsString();
activity.setId(Integer.parseInt(result));
logger.info("result : {} ",result);
}
@After
public void after () throws Exception {
activity.setCreateTime(new Date());
activity.setOrganizer("Update商家");
ResultActions resultAction = mockMvc.perform(MockMvcRequestBuilders.put("/activity")
.contentType(MediaType.APPLICATION_JSON)
.content(JSONUtil.toJsonStr(activity))) ;
MvcResult mvcResult = resultAction.andReturn() ;
String result = mvcResult.getResponse().getContentAsString();
logger.info("result : {} ",result);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ware27-test-junit.zip (17个子文件)
ware27-test-junit
pom.xml 4KB
src
test
java
com
junit
tool
ActivityTest02.java 3KB
ActivityTest01.java 1KB
ActivityTest03.java 1KB
ActivityTest04.java 1KB
ActivityTest05.java 1KB
main
resources
mapper
ActivityMapper.xml 820B
application-dev.yml 307B
table.sql 781B
application.yml 331B
java
com
junit
tool
Application.java 305B
mapper
ActivityMapper.java 246B
controller
ActivityWeb.java 1KB
service
ActivityService.java 198B
ActivityServiceImpl.java 353B
entity
Activity.java 2KB
config
SwaggerConfig.java 1KB
共 17 条
- 1
资源评论
飞翔的佩奇
- 粉丝: 6160
- 资源: 1607
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功