带有spring restdocs 2.0.2.RELEASE的空指针(带有Restassured)



我使用spring-restdocs和spring-restdocs restassured(2.0.2.RELEASE(,来自spring-boot依赖项(2.0.4.RELEASE(。

我使用的代码如下:

@BeforeAll
static void setup(RestDocumentationContextProvider restDocumentation) throws IOException {
spec = new RequestSpecBuilder()
.addFilter(
documentationConfiguration(restDocumentation)
.operationPreprocessors()
.withRequestDefaults(prettyPrint())
.withResponseDefaults(prettyPrint()))
.build();
descriptor = new FieldDescriptor[] {
fieldWithPath("prop1").description("Is property 1"),
fieldWithPath("prop2").description("Is property 2"),
fieldWithPath("prop3").description("Is property 3"),
fieldWithPath("prop4").description("Is property 4"),
fieldWithPath("prop5").description("Is property 5")};
}
@Test
void should_not_be_nullpointer(){
given(spec)
.filter(document("demo",
responseFields().andWithPrefix("[].", descriptor)
))
.port(port)
.basePath("v1")
.when()
.get("/demo")
.then()
.contentType(JSON)
.statusCode(200);
}

我得到以下错误:

java.lang.NullPointerException
at org.springframework.restdocs.ManualRestDocumentation.beforeOperation(ManualRestDocumentation.java:89)
at org.springframework.restdocs.RestDocumentationExtension.lambda$resolveParameter$0(RestDocumentationExtension.java:58)
at org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer.filter(RestAssuredRestDocumentationConfigurer.java:69)
at org.springframework.restdocs.restassured3.RestAssuredOperationPreprocessorsConfigurer.filter(RestAssuredOperationPreprocessorsConfigurer.java:46)
at io.restassured.filter.Filter$filter.call(Unknown Source)

当将spring-restdocs依赖项设置为2.0.1.RELEASE版本时,它可以正常工作。

这似乎是一个bug(我在这里打开了一个问题(,但如果有人对此有更多的见解,那将是最受欢迎的。

Spring REST文档中的上下文是方法范围的,但使用@BeforeAll会使其成为类范围的。由于RestDocumentationExtension是有状态的,您以前没有受到此错误配置的影响。JUnit Jupiter扩展应该是无状态的,所以RestDocumentationExtension是有状态的是一个错误。它在REST Docs 2.0.2中得到了修复,这就是错误配置现在导致问题的原因。

您可以通过将@BeforeAll替换为@BeforeEach来解决此问题(也可以从setup方法中删除static(。有关使用JUnit5时正确设置的更多详细信息,请参阅RESTDocs参考文档。

最新更新