我们有一个使用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的错误(堆栈跟踪为空)。

我们通过使用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()函数。

希望这对别人有帮助!

09-05 12:31
查看更多