2017-04-17 129 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> 
相關問題