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