1lln3ss
1lln3ss

Reputation: 43

Nullpointer with spring-restdocs 2.0.2.RELEASE (with Restassured)

I use spring-restdocs and spring-restdocs-restassured (2.0.2.RELEASE), from spring-boot-dependencies (2.0.4.RELEASE).

The code I use is as follow:

  @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);
  }

I get the following error:

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)

When set spring-restdocs dependencies to version 2.0.1.RELEASE, it works as expected.

This seems to be a bug (I opened an issue here), but if someone has more insight on this, it would be most welcome.

Upvotes: 2

Views: 1200

Answers (1)

Andy Wilkinson
Andy Wilkinson

Reputation: 116061

The context in Spring REST Docs is method-scoped but your use of @BeforeAll makes it class-scoped. You previously got away with this misconfiguration due to RestDocumentationExtension being stateful. JUnit Jupiter extensions should be stateless so RestDocumentationExtension being stateful was a bug. It was fixed in REST Docs 2.0.2 which is why the misconfiguration is now causing a problem.

You can fix the issue by replacing @BeforeAll with @BeforeEach (and also removing static from your setup method). See the REST Docs reference documentation for more details on setting things up correctly when using JUnit 5.

Upvotes: 3

Related Questions