package mvcsample.web;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
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.setup.MockMvcBuilders.*;
import java.util.UUID;
import javax.servlet.http.HttpSession;
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.mock.web.MockHttpSession;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
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.MvcResult;
import org.springframework.test.web.servlet.ResultHandler;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@WebAppConfiguration
@ContextConfiguration(locations={"classpath:/META-INF/spring/web/mvc-config.xml", "classpath:/META-INF/spring/applicationContext.xml", "classpath:/META-INF/spring/applicationContext-jpa.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class MemberRestControllerTest {
private final String createJson = "{\"id\":%d,\"first\":\"%s\",\"last\":\"%s\",\"version\":%d}";
private final String membersJson = "[{\"id\":1,\"first\":\"One\",\"last\":\"One\",\"version\":0},{\"id\":2,\"first\":\"Two\",\"last\":\"Two\",\"version\":0},{\"id\":3,\"first\":\"Three\",\"last\":\"Three\",\"version\":0}]";
@Autowired
private WebApplicationContext wac;
// @Autowired
// private FilterChainProxy springSecurityFilterChain;
private MockMvc mockMvc;
private SessionHolder sessionHolder = new SessionHolder();
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
// .addFilters(springSecurityFilterChain).build();
}
private static final String SEC_CONTEXT_ATTR = HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY;
@Test
public void testAControllerFlow() throws Exception {
// this.mockMvc.perform(post("/j_spring_security_check")
// .param("j_username", "fred")
// .param("j_password", "fredspassword"))
// .andExpect(status().isMovedTemporarily())
// .andDo(new ResultHandler() {
// @Override
// public void handle(MvcResult result) throws Exception {
// sessionHolder.setSession(new SessionWrapper(result.getRequest().getSession()));
// }
// });
this.mockMvc.perform(post("/membersjson").contentType(MediaType.APPLICATION_JSON).content(String.format(createJson, 1,"One","One", 0).getBytes()))
.andExpect(status().isOk());
this.mockMvc.perform(post("/membersjson").contentType(MediaType.APPLICATION_JSON).content(String.format(createJson, 2,"Two","Two", 0).getBytes()))
.andExpect(status().isOk());
this.mockMvc.perform(post("/membersjson").contentType(MediaType.APPLICATION_JSON).content(String.format(createJson, 3,"Three","Three", 0).getBytes()))
.andExpect(status().isOk());
this.mockMvc.perform(get("/membersjson").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(containsString(membersJson)));
this.mockMvc.perform(put("/membersjson").contentType(MediaType.APPLICATION_JSON).content(String.format(createJson, 1,"One","OneUpdated", 0).getBytes()))
.andExpect(status().isOk());
mockMvc.perform(get("/membersjson").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].first").value("One"));
mockMvc.perform(get("/membersjson/1").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(containsString("OneUpdated")));
}
private static final class SessionHolder{
private SessionWrapper session;
public SessionWrapper getSession() {
return session;
}
public void setSession(SessionWrapper session) {
this.session = session;
}
}
private static class SessionWrapper extends MockHttpSession{
private final HttpSession httpSession;
public SessionWrapper(HttpSession httpSession){
this.httpSession = httpSession;
}
@Override
public Object getAttribute(String name) {
return this.httpSession.getAttribute(name);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
spring-mvc-test-sample-master.zip_spring mvc_springmvc-test
共75个文件
png:31个
java:10个
xml:7个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 6 浏览量
2022-09-19
21:00:44
上传
评论
收藏 304KB ZIP 举报
温馨提示
Spring MVC test project.
资源推荐
资源详情
资源评论
收起资源包目录
spring-mvc-test-sample-master.zip (75个子文件)
spring-mvc-test-sample-master
pom.xml 15KB
src
test
java
mvcsample
bean
TestBeanWrapper.java 1KB
web
MemberRestControllerTest.java 5KB
main
webapp
WEB-INF
tags
util
load-scripts.tag 1KB
pagination.tag 4KB
layouts
public.jsp 2KB
layouts.xml 792B
default.jsp 2KB
views
members
list.jsp 3KB
views.xml 720B
create.jsp 1KB
update.jsp 1KB
views.xml 376B
login.jsp 1KB
i18n
application.properties 40B
messages.properties 371B
resources
css
smoothness
images
ui-icons_222222_256x240.png 4KB
ui-icons_454545_256x240.png 4KB
ui-bg_glass_75_dadada_1x400.png 111B
ui-icons_cd0a0a_256x240.png 4KB
ui-bg_glass_75_e6e6e6_1x400.png 110B
ui-icons_888888_256x240.png 4KB
ui-bg_flat_0_aaaaaa_40x100.png 180B
ui-bg_highlight-soft_75_cccccc_1x100.png 101B
ui-bg_glass_65_ffffff_1x400.png 105B
ui-icons_2e83ff_256x240.png 4KB
ui-bg_glass_55_fbf9ee_1x400.png 120B
ui-bg_flat_75_ffffff_40x100.png 178B
ui-bg_glass_95_fef1ec_1x400.png 119B
jquery-ui-1.8.23.custom.css 33KB
images
sort_asc.png 1KB
sort_desc.png 1KB
sort_both.png 1KB
Sorting icons.psd 27KB
sort_desc_disabled.png 1KB
sort_asc_disabled.png 1KB
bootstrap-responsive.min.css 9KB
bootstrap.min.css 79KB
standard.css 824B
bootstrap.css 90KB
bootstrap-responsive.css 12KB
images
resultset_previous.png 389B
create.png 739B
list.png 473B
banner-graphic.png 13KB
show.png 879B
resultset_next.png 395B
resultset_last.png 524B
resultset_first.png 522B
update.png 807B
en.png 599B
delete.png 740B
springsource-logo.png 5KB
add.png 739B
favicon.ico 1KB
js
jquery-1.7.2.min.js 93KB
jquery-1.7.2.js 247KB
bootstrap.min.js 21KB
jquery-ui-1.8.23.custom.min.js 200KB
bootstrap.js 47KB
resources
logback.xml 477B
META-INF
persistence.xml 922B
spring
database.properties 138B
applicationContext-security.xml.backup 2KB
web
mvc-config.xml 3KB
import.sql 1KB
java
mvcsample
config
MvcConfiguration.java 3KB
CustomWebAppInitializer.java 560B
RootConfiguration.java 2KB
domain
Member.java 2KB
Address.java 925B
repositories
MemberRepository.java 562B
web
MemberController.java 3KB
MemberJsonController.java 2KB
.gitignore 55B
共 75 条
- 1
资源评论
小波思基
- 粉丝: 70
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功