我使用spring-restdocs和(2.0.2.RELEASE),使用spring依赖项(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 (我打开了一个问题这里),但如果有人对此有更多的洞察力,这将是最受欢迎的。
发布于 2018-08-06 10:14:33
Spring中的上下文是方法作用域,但是@BeforeAll的使用使它成为类作用域。之前,由于RestDocumentationExtension是有状态的,所以您已经摆脱了这个错误配置。JUnit木星扩展应该是无状态的,所以RestDocumentationExtension是一个有状态的错误。它在RESTDocs2.0.2中得到了修正,这就是为什么错误配置现在导致了一个问题。
您可以通过将@BeforeAll替换为@BeforeEach (以及从setup方法中移除static )来解决此问题。有关在使用参考文献 5时正确设置事情的更多细节,请参见REST Docs JUnit 5。
https://stackoverflow.com/questions/51672764
复制相似问题