Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用Cucumber生成Spring rest文档_Spring_Spring Restdocs - Fatal编程技术网

无法使用Cucumber生成Spring rest文档

无法使用Cucumber生成Spring rest文档,spring,spring-restdocs,Spring,Spring Restdocs,我试图使用SpringCucumberJVM为我们的服务测试SpringREST文档中的RESTAPI,但当我尝试执行该场景时,由于框架无法初始化Junit上下文,因此最终会出现空指针执行 错误消息: java.lang.NullPointerException at org.springframework.restdocs.ManualRestDocumentation.beforeO‌​peration(ManualRestD‌​ocumentation.java:90‌​) at or

我试图使用SpringCucumberJVM为我们的服务测试SpringREST文档中的RESTAPI,但当我尝试执行该场景时,由于框架无法初始化Junit上下文,因此最终会出现空指针执行

错误消息:

java.lang.NullPointerException at 
org.springframework.restdocs.ManualRestDocumentation.beforeO‌​peration(ManualRestD‌​ocumentation.java:90‌​) at 
org.springframework.restdocs.JUnitRestDocumentation.beforeOp‌​eration(JUnitRestDoc‌​umentation.java:76)
代码:

步骤定义代码:

@Given("^create a rest document for VHR API$")
public void create_a_rest_document_for_VHR_API() throws Throwable {
    estAssured.given( spec )
        .accept( "application/json" )
        .filter( document( "vhrdocument" ) ) .when() 
        .get( props.getVhrrequesturl() + "/vhrData/{vehicleID}", "5VW4T7AU0FM029999" ) .then().log().all();
}

您没有按照预期使用的方式使用JUnitRestDocumentation。它被设计为用作JUnit规则,这意味着它应该是一个用
@rule
注释的公共字段:

@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
作为一条规则意味着JUnit将为每个测试自动调用
restDocumentation
,允许SpringREST文档设置和分解特定于测试的上下文。之所以发生
NullPointerException
,是因为没有以这种方式调用
restDocumentation
,因此没有设置上下文


您尚未描述如何使用Cucumber,但如果您使用Cucumber,您应该能够通过将
restDocumentation
声明为
@Rule
-注释字段来解决问题,如上所示。如果您没有使用它的JUnit runner,您可能需要使用SpringREST文档的
ManualRestDocumentation
。Spring REST文档参考文档包含描述如何在不使用JUnit的情况下设置测试的文档。

我遇到了相同的问题,因为我有多个测试类继承了该类,在该类中我声明了
JUnitRestDocumentation
实例。我的错误是我使用
@rule
注释声明了规则。我应该使用
@ClassRule
并将实例声明为
静态

@ClassRule
public static JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();

从RestAssured 2.x迁移到RestAssured 3.1.1时,我也有同样的症状

代码库有一种设置重新设置的方法,以避免每次测试的重复仪式:

@Rule
public JUnitRestDocumentation restDocumentation=新的JUnitRestDocumentation()

在我迁移到3.x之前,它一直运行良好。问题是
newrequestspecbuilder()
将自身附加到默认的静态
RestAssured.requestSpecification

第一个测试通过了,但当它完成时,规则被释放(后面的部分),当第二个测试开始运行时,Before方法是chaining

  • 为第一个测试创建的规范(参考第一个测试方法使用的已处理规则)
  • 为第二个测试创建的规范(参考第二个测试方法的活动规则)
  • 等等,因为新的测试正在运行。 但当第二个测试运行时,按顺序重新启动invoke规范,例如编号1,但因为它引用的是已处理的规则(before操作是在
    null
    上下文上执行的)

    要解决此问题,代码必须清除以前的规范:

    @Before
    public void configure_rest_assured() {
        RestAssured.port = springServerPort;
        RestAssured.config = config().objectMapperConfig(
            objectMapperConfig().jackson2ObjectMapperFactory((cls, charset) -> customObjectMapper)
        )
        ...;
        RestAssured.requestSpecification = null; // avoid the builder to acquire previous specs.
        RestAssured.requestSpecification = new RequestSpecBuilder()
            .addRequestSpecification(documentationConfiguration(docRule, ...))
            ...
            .build();
    }
    

    对于
    cucumber-java-8
    与SpringREST文档和SpringSecurity一起使用,以下内容对我很有用

    这结合了上面@AndyWilkison的答案,但是使用了cumber钩子而不是junit规则

    公共类StepDefs实现En{
    @自动连线
    私有WebApplicationContext上下文;
    私有MockMvc-MockMvc;
    private ManualRestDocumentation restDocumentation=新的ManualRestDocumentation();
    公共StepDefs(){
    BeforeStep((场景)->{
    restDocumentation.beforeTest(AuthenticationStepDefs.class,scenario.getName());
    mockMvc=MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).apply(documentationConfiguration(restDocumentation)).build();
    });
    后续步骤((场景)->{
    restDocumentation.postest();
    });
    当(“为VHR API创建rest文档”时,()->{
    MVC结果=mockMvc.perform(/*
    你在这里的正常通话
    */).
    .andDo(文件(“文件”))。
    .andReturn();
    }
    }
    }
    
    这发生在test SpockFramework上,我将其添加到pom.xml中:

    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-junit4</artifactId>
        <scope>test</scope>
    </dependency>
    
    
    org.spockframework
    spock-junit4
    测试
    
    错误消息:org.springframework.restdocs.ManualRestDocumentation.beforeOperation(ManualRestDocumentation.java:90)org.springframework.restdocs.JUnitRestDocumentation.beforeOperation(JUnitRestDocumentation.java:76)@Autowired private AppProperties;@Before(@rest)public void beforecasenario(){JUnitRestDocumentation restDocumentation=new JUnitRestDocumentation(“目标/生成的代码段”);System.out.println(“JUnitRestDocumentation”+restDocumentation);spec=new RequestSpecBuilder().addFilter(documentationConfiguration(restDocumentation)).build();System.out.println(“\n spec init..”+restDocumentation);}步骤定义代码:@Given(“^create a rest document for VHR API$”)public void create_a_rest_document_for_VHR_API()抛出可丢弃的{restasured.Given(spec).accept(“application/json”).filter(document(“vhrdocument”)).when().get(props.getvhrequesturl()+“/vhrData/{vehicleID}”,“5VW4T7AU0FM029999”)。然后().log().all();}请将注释添加到您的问题中并正确设置格式:)了解您使用的REST文档的版本也会很有用这是最好的答案@没有junit4 runner的规则将不起作用。当前的Spock使用JUnit5引擎,Spock不支持JUnit5扩展。
    @Before
    public void configure_rest_assured() {
        RestAssured.port = springServerPort;
        RestAssured.config = config().objectMapperConfig(
            objectMapperConfig().jackson2ObjectMapperFactory((cls, charset) -> customObjectMapper)
        )
        ...;
        RestAssured.requestSpecification = null; // avoid the builder to acquire previous specs.
        RestAssured.requestSpecification = new RequestSpecBuilder()
            .addRequestSpecification(documentationConfiguration(docRule, ...))
            ...
            .build();
    }
    
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-junit4</artifactId>
        <scope>test</scope>
    </dependency>