Spring Boot Mvc 单元测试

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
@AutoConfigureMockMvc
public class HelloWorldApplicationTests {

	@Autowired
	private MockMvc mockMvc;

	@Test
	public void accessUnprotected() throws Exception {
		// @formatter:off
		this.mockMvc.perform(get("/index"))
				.andExpect(status().isOk());
		// @formatter:on
	}

	@Test
	public void accessProtectedRedirectsToLogin() throws Exception {
		// @formatter:off
		MvcResult mvcResult = this.mockMvc.perform(get("/user/index"))
				.andExpect(status().is3xxRedirection())
				.andReturn();
		// @formatter:on

		assertThat(mvcResult.getResponse().getRedirectedUrl()).endsWith("/login");
	}

	@Test
	public void loginUser() throws Exception {
		// @formatter:off
		this.mockMvc.perform(formLogin().user("user").password("password"))
				.andExpect(authenticated());
		// @formatter:on
	}

	@Test
	public void loginInvalidUser() throws Exception {
		// @formatter:off
		this.mockMvc.perform(formLogin().user("invalid").password("invalid"))
				.andExpect(unauthenticated())
				.andExpect(status().is3xxRedirection());
		// @formatter:on
	}

	@Test
	public void loginUserAccessProtected() throws Exception {
		// @formatter:off
		MvcResult mvcResult = this.mockMvc.perform(formLogin().user("user").password("password"))
				.andExpect(authenticated()).andReturn();
		// @formatter:on

		MockHttpSession httpSession = (MockHttpSession) mvcResult.getRequest().getSession(false);

		// @formatter:off
		this.mockMvc.perform(get("/user/index").session(httpSession))
				.andExpect(status().isOk());
		// @formatter:on
	}

	@Test
	public void loginUserValidateLogout() throws Exception {
		// @formatter:off
		MvcResult mvcResult = this.mockMvc.perform(formLogin().user("user").password("password"))
				.andExpect(authenticated()).andReturn();
		// @formatter:on

		MockHttpSession httpSession = (MockHttpSession) mvcResult.getRequest().getSession(false);

		// @formatter:off
		this.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
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值