From 3bbaab9c2876894188c2c89ba7530e3c6a298c12 Mon Sep 17 00:00:00 2001 From: myh Date: Sun, 3 Dec 2023 23:49:39 +0800 Subject: [PATCH] test spring security config --- .../springdemo/controller/UsersControllerTests.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/example/springdemo/controller/UsersControllerTests.java b/src/test/java/com/example/springdemo/controller/UsersControllerTests.java index 3e07792..1ebc32d 100644 --- a/src/test/java/com/example/springdemo/controller/UsersControllerTests.java +++ b/src/test/java/com/example/springdemo/controller/UsersControllerTests.java @@ -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 {