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

标签 java spring spring-boot rest-assured spring-restdocs

我使用 spring-restdocs 和 spring-restdocs-restassured (2.0.2.RELEASE),来自 spring-boot-dependency (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 时,它会按预期工作。

这似乎是一个错误(我打开了一个问题 here ),但如果有人对此有更多见解,我们将非常欢迎。

最佳答案

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

您可以通过替换 @BeforeAll 来解决该问题与 @BeforeEach (并且还从 static 方法中删除 setup )。请参阅 REST 文档 reference documentation有关使用 JUnit 5 时正确设置的更多详细信息。

关于java - 带有 spring-restdocs 2.0.2.RELEASE 的空指针(带有 Restassured),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51672764/

相关文章:

spring - 不要使用 RepositoryRestResource 插入重复项

java - 是否可以在 Spring 运行时 Autowiring 方法参数

java - 在java中使用readMethod()调用对象的属性?

java - 调整 JFrame 大小时撕裂 - 这可以避免或改进吗?

java - 如何从外部实用程序 jar 正确加载和配置 Spring bean

java - @ bean : unique bean name in subclasses

spring - 使用 Spring Data REST 自定义端点

java - 使用 Azure Active Directory 时如何从 WebSecurityConfigurerAdapter 迁移到 SecurityFilterChain

java - 使用 JDBC 从 Access 数据库读取 Unicode 数据

java - UISpec4J - AbstractMethodError(UISpecToolkit.createKeyboardFocusManagerPeer)