test spring security config
This commit is contained in:
parent
31505cf70b
commit
3bbaab9c28
@ -9,11 +9,16 @@ import jakarta.annotation.Resource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.security.test.context.support.WithUserDetails;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
@ -21,7 +26,8 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@AutoConfigureMockMvc(addFilters = false)
|
||||
// close or open Spring Security configuration
|
||||
@AutoConfigureMockMvc(addFilters = true)
|
||||
@WebMvcTest(UsersController.class)
|
||||
class UsersControllerTests {
|
||||
@Resource
|
||||
@ -46,6 +52,7 @@ class UsersControllerTests {
|
||||
assert usersService != null;
|
||||
}
|
||||
|
||||
@WithMockUser(roles = {"管理员"})
|
||||
@Test
|
||||
public void testGetAllUsers() throws Exception {
|
||||
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/users/find/all"))
|
||||
@ -55,6 +62,7 @@ class UsersControllerTests {
|
||||
logger.info(result.getResponse().getContentAsString());
|
||||
}
|
||||
|
||||
@WithMockUser(roles = {"管理员"})
|
||||
@Test
|
||||
public void testGetUserByName() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/users/find/name").param("name", "admin"))
|
||||
@ -62,6 +70,7 @@ class UsersControllerTests {
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
@WithMockUser(roles = {"用户"})
|
||||
@Test
|
||||
public void testGetUserById() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/users/find/id").param("id", "1"))
|
||||
@ -69,6 +78,7 @@ class UsersControllerTests {
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
@WithMockUser(roles = {"管理员"})
|
||||
@Test
|
||||
@Rollback(value = true)
|
||||
public void testAddUser() throws Exception {
|
||||
@ -148,6 +158,7 @@ class UsersControllerTests {
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
@WithMockUser(username = "测试用户", password = "20211120172", roles = {"用户"})
|
||||
@Test
|
||||
@Rollback(value = true)
|
||||
public void testUpdateUserPassword() throws Exception {
|
||||
|
Loading…
Reference in New Issue
Block a user