繁体   English   中英

使用Spring Boot和Security进行集成测试

[英]Integration Tests Using Spring Boot and Security

我们有一个使用Gradle构建的Spring应用程序,该应用程序通过Spring Boot 1.2.5.RELEASE运行。 我们使用Rest Restured编写了一些初始集成测试,以针对我们的REST端点进行测试。 这样行之有效,我们的应用程序的REST端点通过浏览器和Postman进行了适当的响应。

然后,我们使用Spring Security来实现OncePerRequestFilter和我们自己的AuthenticationProvider。 我们的身份验证工作正常,浏览器和Postman仍在收到适当的响应,但是我们的集成测试不再起作用。

逐步执行测试,我们确实看到我们的Controller端点被调用并返回正确的输出 ,但是在此之后,我们收到org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.AbstractMethodError错误(堆栈跟踪为空) org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.AbstractMethodError org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.AbstractMethodError

我们通过使用Spring Security初始化集成测试而取得了进展,我们尝试放弃Rest Assured,而仅使用MockMvc,我们尝试回到Rest Assured并使用MockMvc进行初始化。 到目前为止没有运气。

我们的初始化代码如下,注释掉的部分用于“确保放心”,当前实现直接使用MockMvc。

任何帮助将不胜感激!

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = IamExtensionApplication.class)
@WebIntegrationTest
public class OurIntegrationTest {
private MockMvc mockMvc;

@Autowired
private WebApplicationContext webApplicationContext;

//    @Autowired
//    private FilterChainProxy filterChainProxy;


@Before
public void setUp() {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).
            apply(springSecurity()).
//                addFilters(filterChainProxy).
            build();

//        RestAssuredMockMvc.mockMvc(mockMvc);
}

@Test
public void our_test() {
    try {
        ResultActions resp = amockMvc.perform(post("/our/endpoint").param("test", "test_value"));
    } catch (Exception e) {
        e.printStackTrace();
    }
//        MockMvcResponse resp = given()
//                .param("test", "test_value")
//                .when()
//                .post("/our/endpoint");
}

我们尝试了这些配置的几种变体,但是我们最终尝试的一种变体(实际上并没有在任何地方找到文档)是将filterChainProxy作为参数传递给springSecurity()函数。

希望这对别人有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM