我有一个带有HeaderHttpSessionStrategy&的弹簧安全设置。自定义UserDetailService。它工作正常,登录在POSTMAN中测试时按预期工作。但是,在使用JUnit进行测试时,它会返回BadCredentials错误。你能帮我解决这个问题吗,因为我需要用经过身份验证的用户来测试我的应用程序。
以下是我的方法:
@Test
public void testLogin() throws Exception {
Map<String, String> credentials = new HashMap<>();
credentials.put("username", testUsername);
credentials.put("password", testPassword);
mockMvc.perform(post("/login").content(mapper.writeValueAsBytes(credentials))
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.authToken").isNotEmpty());
}