Spring Boot Mvc 单元测试
来源:互联网 发布:淘宝创业计划书模板 编辑:程序博客网 时间:2024/06/04 05:48
1、开发 都是需要保存测试代码的,mvc也不例外,都需要写测试代码,下面写了一个简单的mvc 但愿测试的代码.
import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.mock.web.MockHttpSession;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.MvcResult;import static org.assertj.core.api.Assertions.assertThat;import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;/** * * @author Joe Grandja *///Junit 启动类@RunWith(SpringJUnit4ClassRunner.class)//Spring Boot Test自动配置@SpringBootTest//自动配置,注入 Mock Mvc@AutoConfigureMockMvcpublic class HelloWorldApplicationTests {@Autowiredprivate MockMvc mockMvc;@Testpublic void accessUnprotected() throws Exception {// @formatter:offthis.mockMvc.perform(get("/index")).andExpect(status().isOk());// @formatter:on}@Testpublic void accessProtectedRedirectsToLogin() throws Exception {// @formatter:offMvcResult mvcResult = this.mockMvc.perform(get("/user/index")).andExpect(status().is3xxRedirection()).andReturn();// @formatter:onassertThat(mvcResult.getResponse().getRedirectedUrl()).endsWith("/login");}@Testpublic void loginUser() throws Exception {// @formatter:offthis.mockMvc.perform(formLogin().user("user").password("password")).andExpect(authenticated());// @formatter:on}@Testpublic void loginInvalidUser() throws Exception {// @formatter:offthis.mockMvc.perform(formLogin().user("invalid").password("invalid")).andExpect(unauthenticated()).andExpect(status().is3xxRedirection());// @formatter:on}@Testpublic void loginUserAccessProtected() throws Exception {// @formatter:offMvcResult mvcResult = this.mockMvc.perform(formLogin().user("user").password("password")).andExpect(authenticated()).andReturn();// @formatter:onMockHttpSession httpSession = (MockHttpSession) mvcResult.getRequest().getSession(false);// @formatter:offthis.mockMvc.perform(get("/user/index").session(httpSession)).andExpect(status().isOk());// @formatter:on}@Testpublic void loginUserValidateLogout() throws Exception {// @formatter:offMvcResult mvcResult = this.mockMvc.perform(formLogin().user("user").password("password")).andExpect(authenticated()).andReturn();// @formatter:onMockHttpSession httpSession = (MockHttpSession) mvcResult.getRequest().getSession(false);// @formatter:offthis.mockMvc.perform(post("/logout").with(csrf()).session(httpSession)).andExpect(unauthenticated());this.mockMvc.perform(get("/user/index").session(httpSession)).andExpect(unauthenticated()).andExpect(status().is3xxRedirection());// @formatter:on}}
0 0
- spring boot mvc单元测试
- Spring Boot Mvc 单元测试
- Spring Boot 单元测试
- Spring Boot Junit单元测试
- Spring Boot Junit单元测试
- spring boot 单元测试
- Spring Boot Junit单元测试
- spring boot 测试 单元测试
- Spring Boot Junit单元测试
- Spring Boot Junit单元测试
- Spring Boot Junit单元测试
- Spring Boot单元测试
- spring boot单元测试
- spring boot 单元测试
- Spring Boot单元测试
- Spring boot 单元测试
- spring boot单元测试
- spring-boot单元测试
- 微信公众号中增加地图位置标识
- 记录一次Centos磁盘空间占满的解决办法
- 常用工具类
- 初探Spring Security
- spring boot 采用https启动
- Spring Boot Mvc 单元测试
- 浅看消息中间件
- 求两个字符串的最长公共子串——Java实现
- Spring security 安全设置
- tensorflow 中 name_scope 及 variable_scope 的异同
- jquery中attr和prop的区别,tab和click的区别,js中的call和apply方法的区别.
- ansible 安装 httpd-2.4.25 启动httpd
- Excel转换成DataTable DataTable转换成Excel
- yum install