Merge pull request #14 from nagisa77/codex/fix-customaccessdeniedhandler-not-invoked

Fix AccessDeniedHandler usage in MVC tests
This commit is contained in:
Tim
2025-06-30 20:46:35 +08:00
committed by GitHub
2 changed files with 4 additions and 8 deletions

View File

@@ -7,7 +7,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.web.access.AccessDeniedHandler; import com.openisle.config.CustomAccessDeniedHandler;
import com.openisle.config.SecurityConfig; import com.openisle.config.SecurityConfig;
import com.openisle.service.JwtService; import com.openisle.service.JwtService;
import com.openisle.repository.UserRepository; import com.openisle.repository.UserRepository;
@@ -22,7 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@WebMvcTest(AdminController.class) @WebMvcTest(AdminController.class)
@AutoConfigureMockMvc @AutoConfigureMockMvc
@Import(SecurityConfig.class) @Import({SecurityConfig.class, CustomAccessDeniedHandler.class})
class AdminControllerTest { class AdminControllerTest {
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@@ -31,8 +31,6 @@ class AdminControllerTest {
private JwtService jwtService; private JwtService jwtService;
@MockBean @MockBean
private UserRepository userRepository; private UserRepository userRepository;
@MockBean
private AccessDeniedHandler customAccessDeniedHandler;
@Test @Test
void adminHelloReturnsMessage() throws Exception { void adminHelloReturnsMessage() throws Exception {

View File

@@ -7,7 +7,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.web.access.AccessDeniedHandler; import com.openisle.config.CustomAccessDeniedHandler;
import com.openisle.config.SecurityConfig; import com.openisle.config.SecurityConfig;
import com.openisle.service.JwtService; import com.openisle.service.JwtService;
import com.openisle.repository.UserRepository; import com.openisle.repository.UserRepository;
@@ -22,7 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@WebMvcTest(HelloController.class) @WebMvcTest(HelloController.class)
@AutoConfigureMockMvc @AutoConfigureMockMvc
@Import(SecurityConfig.class) @Import({SecurityConfig.class, CustomAccessDeniedHandler.class})
class HelloControllerTest { class HelloControllerTest {
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@@ -31,8 +31,6 @@ class HelloControllerTest {
private JwtService jwtService; private JwtService jwtService;
@MockBean @MockBean
private UserRepository userRepository; private UserRepository userRepository;
@MockBean
private AccessDeniedHandler customAccessDeniedHandler;
@Test @Test
void helloReturnsMessage() throws Exception { void helloReturnsMessage() throws Exception {