2017-04-17 72 views
0

我的目标是为oauth编写单元测试 - 检查用户名和标记是否正确。在Spring引导框架中为Oauth编写单元测试

搜索使我这个ARTICAL Faking OAuth SSO

现在,我下面战略#1。

以下是我需要找出它们没有直接提到的依赖关系的代码片段。

@Test 
public void testGetAuthenticationInfo() throws Exception { 
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) 
      .apply(springSecurity()) 
      .build(); 

    mockMvc.perform(MockMvcRequestBuilders.get("/api/token") 
      .with(authentication(getOauthTestAuthentication())) 
      .sessionAttr("scopedTarget.oauth2ClientContext", getOauth2ClientContext())) 
      .andExpect(status().isOk()) 
      .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) 
      .andExpect(jsonPath("$.username").value("bwatkins")) 
      .andExpect(jsonPath("$.token").value("my-fun-token")); 
} 

我设法为web应用上下文如下安排 -

@Autowired 
private WebApplicationContext webApplicationContext; 

对于springSecurity()方法,我无法得到正确的依赖。无论我搜索的是什么,都让我相信导入org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers。*会给我这个方法,但我无法获得正确的pom依赖。

问题1:任何人都可以帮助我获得依赖吗? 问题2:对于OAuth这样的标准事件,是不是有一个标准的弹簧启动测试包,应该只需要配置就可以测试oauth是否工作正常

回答

0

这是前面提到的问题春季安全github。它花了一些时间来找出正确的版本,这是4.0.2.RELEASE

https://github.com/spring-projects/spring-security/issues/3861

添加在pom.xml中以下固定的问题。

 <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-test</artifactId> 
     <version>4.0.2.RELEASE</version> 
    </dependency>