首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带spring-restdocs 2.0.2的Null指针(带restdocs)

带spring-restdocs 2.0.2的Null指针(带restdocs)
EN

Stack Overflow用户
提问于 2018-08-03 12:33:19
回答 1查看 978关注 0票数 2

我使用spring-restdocs和(2.0.2.RELEASE),使用spring依赖项(2.0.4.RELEASE)。

我使用的代码如下:

代码语言:javascript
复制
  @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);
  }

我得到以下错误:

代码语言:javascript
复制
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 (我打开了一个问题这里),但如果有人对此有更多的洞察力,这将是最受欢迎的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-06 10:14:33

Spring中的上下文是方法作用域,但是@BeforeAll的使用使它成为类作用域。之前,由于RestDocumentationExtension是有状态的,所以您已经摆脱了这个错误配置。JUnit木星扩展应该是无状态的,所以RestDocumentationExtension是一个有状态的错误。它在RESTDocs2.0.2中得到了修正,这就是为什么错误配置现在导致了一个问题。

您可以通过将@BeforeAll替换为@BeforeEach (以及从setup方法中移除static )来解决此问题。有关在使用参考文献 5时正确设置事情的更多细节,请参见REST Docs JUnit 5。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51672764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档