boot应用程序,需要登录才能执行某些操作.我试图使用MockMvc测试它们,但它似乎不起作用.我一直收到状态为403的HTTP响应(禁止).可能是身份验证部分出了问题.
我试过跟随 documentation ,但我无法让它工作.
这是我目前的测试代码:
@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实现,您可能会获得类强制转换异常.如果您没有从自定义UserDetailsService返回自定义类型,则此解决方案应该可以正常工作.
@Test @WithMockUser(username="admin",roles={"USER","ADMIN"}) public void changePasswordWorks() throws Exception {
@WithUserDetails
如果您实现了返回UserDetails的自定义实现的自定义UserDetailsService,则此解决方案可能适合您.
要使它工作,您需要将UserDetailsService公开为Bean,并且用户必须存在.例如:
@Test @WithUserDetails("admin") public void changePasswordWorks() throws Exception {
@WithSecurityContext
这是两个世界中最好的,但需要一些额外的设置.如果您有一个自定义UserDetailsService返回UserDetails的自定义实现,并且不希望用户必须存在,则可以使用此方法.我会让你阅读有关此设置的文档,因为它有点冗长且记录完备.
Using a 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"); }
翻译自:https://stackoverflow.com/questions/29704428/run-unit-tests-on-controllers-that-require-authentication