When I tried to use JUnit’s Mock to test the whitelist API, Security intercepted it. I found that the matching machine that should have been MvcRequestMatcher turned into SpringAntMatcher. Because when registering RequestMathcer, SpringBootMockServlet returned an empty Registration Map, which caused all RequestMatchers to be registered as SpringAntMatcher. How can I solve this problem.
enter image description here
This is the correct RequestMatcher:
enter image description here
[enter image description here](https://i.sstatic.net/JtQ51P2C.png)
This is fail:
enter image description here
Environment(for bug reports)
- Security version:6.2.0
- JUnit version: 5.10.3
- SpringBoot version:3.2.0
Test code:
@SpringBootTest(classes = {ElectricDriveParkApplication.class})
@AutoConfigureMockMvc
public class UserRegisterTestPj {
@Autowired
private UserRegisterService userRegisterService;
@Autowired
private JedisUtils jedisUtils;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private MockMvc mockMvc;
private String RG_TOKEN = PrefixDefinition.USER_REGISTER_TOKEN_CODE_PREFIX_;
private String RG_EMAIL = PrefixDefinition.USER_REGISTER_EMAIL_;
@Test
@DisplayName("User register fail test")
void test_user_register_fail() throws Exception {
UserFirstRegisterVO userFirstRegisterVO = new UserFirstRegisterVO();
userFirstRegisterVO.setEmail("[email protected]");
userFirstRegisterVO.setPasswd("12345678");
userFirstRegisterVO.setCapChatToken("123");
String jsonRequest = objectMapper.writeValueAsString(userFirstRegisterVO);
MvcResult result = mockMvc.perform(
MockMvcRequestBuilders.post("/dev/user/register")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonRequest)
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
String jsonResponse = result.getResponse().getContentAsString();
ResultVO response = objectMapper.readValue(jsonResponse, ResultVO.class);
assertNotNull(response);
assertEquals(200, response.getCode());
assertTrue((Boolean) response.getData());
}
}
enter image description here
I tried to replace ServletContext, but encountered a 404 error again.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
2024-08-05 17:29:35,276 INFO [main] LogsInterceptor-----request method: [POST] request URL: http://localhost/dev/user/register and request param: {"email":"[email protected]","passwd":"12345678","capChatToken":"123"}
java.lang.AssertionError: Status expected:<200> but was:<404>
Expected :200
Actual :404
<Click to see difference>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:637)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:214)
at user.UserRegisterTestPj.test_user_register_fail(UserRegisterTestPj.java:69)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Its code:
@SpringBootTest(classes = {ElectricDriveParkApplication.class})
//@AutoConfigureMockMvc
public class UserRegisterTestPj {
@Autowired
private UserRegisterService userRegisterService;
@Autowired
private JedisUtils jedisUtils;
@Autowired
private ObjectMapper objectMapper;
// @Autowired
private static MockMvc mockMvc;
private String RG_TOKEN = PrefixDefinition.USER_REGISTER_TOKEN_CODE_PREFIX_;
private String RG_EMAIL = PrefixDefinition.USER_REGISTER_EMAIL_;
@BeforeAll
public static void setUp(@Autowired GenericWebApplicationContext context) {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
@DisplayName("User register fail test")
void test_user_register_fail() throws Exception {
UserFirstRegisterVO userFirstRegisterVO = new UserFirstRegisterVO();
userFirstRegisterVO.setEmail("[email protected]");
userFirstRegisterVO.setPasswd("12345678");
userFirstRegisterVO.setCapChatToken("123");
String jsonRequest = objectMapper.writeValueAsString(userFirstRegisterVO);
MvcResult result = mockMvc.perform(
MockMvcRequestBuilders.post("/dev/user/register")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonRequest)
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
String jsonResponse = result.getResponse().getContentAsString();
ResultVO response = objectMapper.readValue(jsonResponse, ResultVO.class);
assertNotNull(response);
assertEquals(200, response.getCode());
assertTrue((Boolean) response.getData());
}
}
user24847981 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.