java – 在需要身份验证的控制器上运行单元测试

栏目: 编程工具 · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/29704428/run-unit-tests-on-controllers-that-require-authentication
我有一个 spring

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实现,您可能会获得类强制转换异常.如果您没有从自定义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的自定义实现,并且不希望用户必须存在,则可以使用此方法.我会让你阅读有关此设置的文档,因为它有点冗长且记录完备.

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


以上所述就是小编给大家介绍的《java – 在需要身份验证的控制器上运行单元测试》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

500 Lines or Less

500 Lines or Less

Amy Brown、Michael DiBernardo / 2016-6-28 / USD 35.00

This book provides you with the chance to study how 26 experienced programmers think when they are building something new. The programs you will read about in this book were all written from scratch t......一起来看看 《500 Lines or Less》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具