在需要身份validation的控制器上运行unit testing

我有一个spring boot应用程序,需要登录才能执行某些操作。 我试图使用MockMvc测试它们,但它似乎不起作用。 我一直收到状态为403的HTTP响应(禁止)。 可能是身份validation部分出了问题。

我已经尝试过遵循文档 ,但我无法使其正常工作。

这是我目前的测试代码:

 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {Application.class}) @WebIntegrationTest("server.port = 8093") public class PasswordChangeTests { @Autowired private EmbeddedWebApplicationContext webApplicationContext; @Autowired private UserRepository userRepository; private MockMvc mockMvc; @Before public void setUp() throws Exception { this.mockMvc = MockMvcBuilders .webAppContextSetup(webApplicationContext) .apply(springSecurity()) .build(); } @Test public void changePasswordWorks() throws Exception { // Send password change request PasswordChangeRepresentation passwordChange = new PasswordChangeRepresentation(DefaultUsers.Admin.getPassword(), "12345678"); mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.POST, "/password/change") .content(new ObjectMapper().writeValueAsString(passwordChange)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); // Check that the password has been changed User user = this.userRepository.findByUsername(DefaultUsers.Admin.getEmail()); Assert.assertEquals(user.getPassword(), "12345678"); } } 

对不起,如果我遗漏了明显的东西。 这是我第一次使用春季靴子。

您需要指定要将测试运行的用户。 您有几个选项(每个选项都是详细文档的链接):

@WithMockUser

此选项将创建虚假用户(即用户不需要存在于数据存储中)。 这种方法的问题是如果您的应用程序依赖于自定义User实现,您可能会获得类强制转换exception。 如果您没有从自定义UserDetailsS​​ervice返回自定义类型,则此解决方案应该可以正常工作。

  @Test @WithMockUser(username="admin",roles={"USER","ADMIN"}) public void changePasswordWorks() throws Exception { 

@WithUserDetails

如果您实现了返回UserDetails的自定义实现的自定义UserDetailsS​​ervice,则此解决方案可能适合您。

要使它工作,您需要将UserDetailsS​​ervice公开为Bean,并且用户必须存在。 例如:

  @Test @WithUserDetails("admin") public void changePasswordWorks() throws Exception { 

@WithSecurityContext

这是两个世界中最好的,但需要一些额外的设置。 如果您有一个自定义UserDetailsS​​ervice返回UserDetails的自定义实现,并且不希望用户必须存在,则可以使用此方法。 我会让你阅读有关此设置的文档,因为它有点冗长且记录完备。

使用RequestPostProcessor

如果注释不是你的东西,你可以使用RequestPostProcessor。 例如:

 import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; ... @Test public void changePasswordWorks() throws Exception { // Send password change request PasswordChangeRepresentation passwordChange = new PasswordChangeRepresentation(DefaultUsers.Admin.getPassword(), "12345678"); mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.POST, "/password/change") // ADD this line .with(user("admin").roles("USER","ADMIN")) .content(new ObjectMapper().writeValueAsString(passwordChange)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); // Check that the password has been changed User user = this.userRepository.findByUsername(DefaultUsers.Admin.getEmail()); Assert.assertEquals(user.getPassword(), "12345678"); }