SpringSecurity + MockMvc寫單元測試(包括認證,授權,模拟Cookie)
- 寫在前面
- 一、@WithMockUser,基于注解使用
- 1.1、代碼示例
- 二、配置自定義Token
- 2.1、代碼示例
- 關于MockMvc更詳細的使用介紹
- 參考這裡,連結
寫在前面
Spring Security 是一個權限控制架構,所有的接口都是基于目前使用者角色,身份等,授權通路,在開發時,要先登陸,才能測試需要授權的接口, 但是如何寫單元測試,測試授權的接口呢 ?
需要測試依賴,引包,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
具體使用,包括以下幾種測試方式
一、@WithMockUser,基于注解使用
1.1、代碼示例
import lombok.SneakyThrows;
import org.junit.Before;
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.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import java.io.File;
import java.io.FileInputStream;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
/**
* 單元測試-角色子產品接口
* 資料復原 @Transactional
*/
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(classes = Application.class)
//@SpringBootTest
//@FixMethodOrder(MethodSorters.NAME_ASCENDING)
//@Transactional
//@WithMockUser(username = "zx", password = "11111111")
@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
@AutoConfigureMockMvc
@WithMockUser(username = "as", password = "as")
public class UserControllerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders
.webAppContextSetup(wac)
.build();
}
@Test
public void add() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/test/user/add")
.contentType(MediaType.APPLICATION_JSON)
.content("{\n" +
" \"userCode\": \"lss002\",\n" +
" \"userName\": \"ls022\",\n" +
" \"departmentId\": 10,\n" +
" \"email\": \"[email protected]\",\n" +
" \"station\": \"dep\",\n" +
" \"company\": \"com1\",\n" +
" \"entryTime\": \"2001-12-12\",\n" +
" \"leaveTime\": \"\",\n" +
" \"entranceStatus\": 1\n" +
"}")
.accept(MediaType.APPLICATION_JSON))
.andDo(print());
}
}
二、配置自定義Token
2.1、代碼示例
import lombok.SneakyThrows;
import org.junit.Before;
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.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import java.io.File;
import java.io.FileInputStream;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
/**
* 單元測試-角色子產品接口
* 資料復原 @Transactional
*/
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(classes = Application.class)
//@SpringBootTest
//@FixMethodOrder(MethodSorters.NAME_ASCENDING)
//@Transactional
//@WithMockUser(username = "zx", password = "11111111")
@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
@AutoConfigureMockMvc
public class UserControllerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders
.webAppContextSetup(wac)
.apply(springSecurity()) // 這裡配置Security認證
.build();
}
@Test
public void add() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/test/user/add")
.cookie(new Cookie(LoginController.HEADER, "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ6eCIsImV4cCI6MTU4MzQwMzA0NX0.itXLLL8hcjyE-Q13CXl387Xk9-M9PkrmYeayGu-zSa2mZvYUlJx8zNPn81l2Tn9f9Iz1RsyLnf08mH85xD6mkA"))
.contentType(MediaType.APPLICATION_JSON)
.content("{\n" +
" \"userCode\": \"lss002\",\n" +
" \"userName\": \"ls022\",\n" +
" \"departmentId\": 10,\n" +
" \"email\": \"[email protected]\",\n" +
" \"station\": \"dep\",\n" +
" \"company\": \"com1\",\n" +
" \"entryTime\": \"2001-12-12\",\n" +
" \"leaveTime\": \"\",\n" +
" \"entranceStatus\": 1\n" +
"}")
.accept(MediaType.APPLICATION_JSON))
.andDo(print());
}
}