【问题标题】:Writing Unit Tests for Oauth in spring boot framework在 Spring Boot 框架中为 Oauth 编写单元测试
【发布时间】:2017-04-20 10:17:45
【问题描述】:

我的目标是为 oauth 编写单元测试 - 检查用户名和令牌是否正确。

搜索使我找到了这篇文章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"));
}

我设法安排如下 webApplicationContext -

@Autowired
private WebApplicationContext webApplicationContext;

对于 springSecurity() 方法,我无法获得正确的依赖关系。无论我搜索什么,都让我相信导入 org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.* 会为我提供方法,但我无法正确获取 pom 依赖项。

问题 1:任何人都可以帮助我解决依赖问题吗? 问题2:对于像OAuth这样的标准东西,是否有一个标准的spring boot测试包只需要配置来测试oauth是否工作正常

【问题讨论】:

    标签: spring-mvc spring-boot oauth-2.0 google-oauth


    【解决方案1】:

    这是之前在 spring-security 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>
    

    【讨论】:

      猜你喜欢
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 2015-05-17
      • 1970-01-01
      • 2020-02-27
      • 2019-11-05
      • 1970-01-01
      相关资源
      最近更新 更多