2016-08-15 115 views
0

概述如何mvcmock单元测试

我有注射认证REST服务,我想用mockmvc为它创建一个单元测试验证注入。我RestController类如下:

import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Set; 

import org.springframework.security.core.Authentication; 
import org.springframework.security.core.GrantedAuthority; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

import com.blss.security.securityCommon.entities.SecureOperatorDetails; 
import com.blss.security.securityGateway.providers.AccAuthenticationProvider; 

import lombok.Data; 

@RestController 
@RequestMapping("/gateway") 
public class AuthenticationDetailsRestController { 

    @RequestMapping(value = "/userdetails", method = RequestMethod.GET) 
    public UserDetailsResource currentUserName(Authentication authentication) { 

     ArrayList<String> rolesList = new ArrayList<>(); 
     Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); 
     authorities 
      .forEach(a -> { 
       if (!a.getAuthority().startsWith(
        AccAuthenticationProvider.CAD_TOKEN_AUTHORITY_PREFIX)) { 
        rolesList.add(a.getAuthority()); 
       } 
      }); 

     SecureOperatorDetails operator = ((SecureOperatorDetails) authentication.getDetails()); 

     return new UserDetailsResource(
       authentication.getName(), 
       operator.getOperator().getName(), 
       operator.getOperator().getPermittedRetailerIds(), 
       operator.getOperator().getStores(), 
       operator.getOperator().getRetailerId(), 
       operator.getDefaultStoreId(), 
       operator.getDeviceId(), 
       rolesList); 
    } 

    @Data 
    static class UserDetailsResource { 
     private final String username; 
     private final String name; 
     private final Set<String> retailerIds; 
     private final Set<String> stores; 
     private final String retailerId; 
     private final String storeId; 
     private final String deviceId; 
     private final ArrayList<String> roles; 
    } 
} 

问题

我不知道如何嘲笑认证,并注入它在我的测试类,以避免401 HTTP异常访问此资源需要完整身份验证

现在,我将不胜感激,如果有人能帮助我解决这个问题

+0

bypassing authentication?听起来很可疑。 – Takarii

回答

1

我在测试类中添加了以下代码片段,然后正确注入了身份验证:

  1. 添加这有一个定制的认证:

    private MockMvc mockMvc; 
    
    @Autowired 
    private Authentication authentication; 
    @Bean 
    public Authentication authentication() { 
        Collection<GrantedAuthority> authorities = new ArrayList<>(); 
        authorities.add(new SimpleGrantedAuthority("ROLE_USER")); 
        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("customUsername", "customPassword", authorities); return authentication; } 
    
    • 添加以下设置方法:

      @Before
      公共无效设定()抛出异常{
      SecurityContextHolder中。 。的getContext()setAuthentication(认证); mockMvc = MockMvcBuilders.webAppContextSetup(上下文) .addFilter(新ShallowEtagHeaderFilter()) 。适用(documentationConfiguration(restDocumentation)) 。适用(springSecurity()) .build(); }


注:

  • springSecurity(),以使认证用注射;否则,认证对象将在主类中为null(您为其编写的类测试它)。

  • SecurityContextHolder.getContext()。setAuthentication(authentication);用于注入定制认证;否则,默认的一个会被springSecurity注入()

1

您可以使用org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers

然后配置身份验证模拟,你应该初始化它作为下一个:

private MockMvc mockMvc; 

@Override 
protected void before() { 
    this.mockMvc = webAppContextSetup(context).apply(SecurityMockMvcConfigurers.springSecurity()).build(); 
}