package com.example.test;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
//首先指定Junit的Runner
@RunWith(SpringJUnit4ClassRunner.class)
// 指明配置文件所在
@ContextConfiguration(locations = { "classpath:spring-mybatis.xml", "classpath:spring-mvc.xml" })
@WebAppConfiguration
public class MockMvcTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}
// @Test
// public void testHelloController() throws Exception {
// // get查一下列表应该为空。
// RequestBuilder request = null;
//
// request = MockMvcRequestBuilders.get("/hello")
// .accept(MediaType.parseMediaType("application/json;charset=UTF-8"));
// mockMvc.perform(request).andExpect(status().isOk()).andDo(MockMvcResultHandlers.print());
//
// request = MockMvcRequestBuilders.get("/hello2")
// .accept(MediaType.parseMediaType("application/json;charset=UTF-8"));
// mockMvc.perform(request).andExpect(status().isOk()).andDo(MockMvcResultHandlers.print());
// }
@Test
public void testStudentController() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/student/list.do"))
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print());
// get查一下列表应该为空。
// RequestBuilder request = null;
// request = MockMvcRequestBuilders.get("/student/list.do")
// .accept(MediaType.parseMediaType("application/json;charset=UTF-8"));
// mockMvc.perform(request)
// .andExpect(status().isOk())//get方式提交 成功状态为 Status = 200 等同
// status().isOk()
// .andExpect(forwardedUrl("/WEB-INF/jsp/list.jsp"))
// .andExpect(view().name("list"))//执行完后写断言
// .andDo(MockMvcResultHandlers.print());
// 插入一个数据
// request = MockMvcRequestBuilders.post("/student/add.do")
// .param("name", "XH")
// .param("niceName", "XH1995")
// .param("age", "24");
// mockMvc.perform(request)
// .andDo(MockMvcResultHandlers.print())
// .andExpect(status().isFound()) //post提交方式 成功状态为 Status = 302 等同
// .isFound()
// .andExpect(redirectedUrl("/student/list.do"))
// .andExpect(view().name("redirect:/student/list.do"));
/*
* 执行添加后打印响应信息如下: ModelAndView: View name = redirect:/student/list.do View =
* null Attribute = user value = Student [id=141, name=XH, niceName=XH1995,
* age=24] errors = [] FlashMap: Attributes = null
*
* MockHttpServletResponse: Status = 302 Error message = null Headers =
* {Location=[/student/list.do]} Content type = null Body = Forwarded URL =
* null Redirected URL = /student/list.do Cookies = []
*/
// 检查刚才插入的数据
// request = MockMvcRequestBuilders.get("/student/101/show.do");
// mockMvc.perform(request)
// .andDo(MockMvcResultHandlers.print())
// .andExpect(status().isOk()) //get方式提交 成功状态为 Status = 200 等同
// status().isOk()
// .andExpect(forwardedUrl("/WEB-INF/jsp/detail.jsp"))
// .andExpect(view().name("detail"))//执行完后写断言
// .andExpect(model().attributeExists("user"));
//
// //测试put方法
// request = post("/student/101/save.do")
// .param("id", "101")
// .param("name", "Tom123")
// .param("niceName", "Tom123")
// .param("age", "24");
// mockMvc.perform(request)
// .andDo(MockMvcResultHandlers.print())//先执行打印响应信息,然后写断言
// .andExpect(status().isFound()) //Status = 302 等同 .isFound()
// .andExpect(redirectedUrl("/student/list.do"))
// .andExpect(view().name("redirect:/student/list.do"));
//
// //测试删除用户
// request=get("/student/133/del.do");
// mockMvc.perform(request)
// .andDo(MockMvcResultHandlers.print())//先执行打印响应信息,然后写断言
// .andExpect(status().isFound()) //Status = 302 等同 .isFound()
// .andExpect(redirectedUrl("/student/list.do"))
// .andExpect(view().name("redirect:/student/list.do"));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
spring+springmvc+mybatis+jsp练习demo项目源码
共71个文件
java:15个
class:15个
xml:12个
2星 需积分: 27 27 下载量 24 浏览量
2017-11-27
15:23:16
上传
评论 3
收藏 143KB RAR 举报
温馨提示
spring boot+mybatis+jsp 框架组合练习学习做的一个完整的精简的小demo项目源码
资源推荐
资源详情
资源评论
收起资源包目录
spring-springmvc-mybatis.rar (71个子文件)
spring-springmvc-mybatis
pom.xml 5KB
target
test-classes
m2e-wtp
web-resources
META-INF
maven
com.xh.ssm
spring-springmvc-mybatis
pom.xml 5KB
pom.properties 297B
MANIFEST.MF 114B
classes
mybatis-config.xml 507B
database.properties 355B
spring-mybatis.xml 3KB
spring-mvc.xml 2KB
log4j.properties 770B
com
example
dao
TestDao2.class 2KB
TestDao.class 2KB
StudentDao.class 411B
model
Student.class 2KB
test
MockMvcTest.class 3KB
DebugTest.class 837B
UnitTest.class 2KB
UnitTestTest.class 1KB
mapping
StudentMapper.xml 3KB
controller
HelloController.class 1KB
StudentController.class 4KB
service
TestService3.class 2KB
StudentService.class 423B
TestService2.class 2KB
TestService.class 2KB
impl
StudentServiceImpl.class 1KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.core.resources.prefs 140B
org.eclipse.wst.common.component 713B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 50B
.jsdtscope 639B
org.eclipse.jdt.core.prefs 736B
src
test
java
main
webapp
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 2KB
jsp
list.jsp 2KB
detail.jsp 1KB
add.jsp 2KB
index.jsp 57B
edit.jsp 2KB
lib
static
jquery.min.js 91KB
bootstrap
css
bootstrap.min.css 118KB
bootstrap.css 143KB
bootstrap-theme.min.css 23KB
js
bootstrap.min.js 36KB
bootstrap.js 68KB
index.jsp 57B
resources
mybatis-config.xml 507B
database.properties 355B
spring-mybatis.xml 3KB
spring-mvc.xml 2KB
log4j.properties 770B
com
example
mapping
StudentMapper.xml 3KB
java
com
example
dao
StudentDao.java 409B
TestDao2.java 2KB
TestDao.java 2KB
model
Student.java 1KB
test
UnitTest.java 2KB
DebugTest.java 380B
UnitTestTest.java 830B
MockMvcTest.java 5KB
controller
StudentController.java 3KB
HelloController.java 645B
service
TestService.java 2KB
TestService3.java 2KB
TestService2.java 2KB
impl
StudentServiceImpl.java 883B
StudentService.java 417B
.project 1KB
.classpath 2KB
共 71 条
- 1
资源评论
- ottowater2019-01-11项目我看了,里面有报错
PandaCode辉
- 粉丝: 93
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- IMG_20240424_183805.jpg
- DatabaseWatermarkingBasedonTextFormat
- -移动通信-网络课程设计与研究
- 基于Python的PCA人脸识别算法的原理及实现代码+文档详解.zip
- 甘肃移动全业务工程建设项目管理流程优化研究
- 549springboot + vue 民宿管理平台.zip (可运行源码+数据库文件+文档)
- ZArchiver.Pro_0.9.5.apk
- vmware环境配置.mp4
- 548springboot + vue 大学生社团活动平台.zip(可运行源码+数据库文件+文档)
- 微信小程序 辩论倒计时小程序源码 作业设计demo 计算机专业参考
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功