【发布时间】:2017-11-21 21:20:24
【问题描述】:
我正在尝试使用 spring cucumber jvm 测试我们服务的 rest API 的 spring rest 文档,但是当我尝试执行该场景时,最终会出现空指针异常,因为框架无法初始化 Junit 上下文。
错误信息:
java.lang.NullPointerException at
org.springframework.restdocs.ManualRestDocumentation.beforeOperation(ManualRestDocumentation.java:90) at
org.springframework.restdocs.JUnitRestDocumentation.beforeOperation(JUnitRestDocumentation.java:76)
代码:
private AppProperties props;
@Before("@rest") public void beforeScenario() {
JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation( "target/generated-snippets" );
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() throws Throwable {
estAssured.given( spec )
.accept( "application/json" )
.filter( document( "vhrdocument" ) ) .when()
.get( props.getVhrrequesturl() + "/vhrData/{vehicleID}", "5VW4T7AU0FM029999" ) .then().log().all();
}
【问题讨论】:
-
错误消息:org.springframework.restdocs.JUnitRestDocumentation.beforeOperation(JUnitRestDocumentation.java:76) 中 org.springframework.restdocs.ManualRestDocumentation.beforeOperation(ManualRestDocumentation.java:90) 中的 java.lang.NullPointerException
-
@Autowired 私有 AppProperties 道具; @Before("@rest") public void beforeScenario() { JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("target/generated-sn-ps" ); System.out.println("jUnitRestDocumentation" +restDocumentation); spec = new RequestSpecBuilder().addFilter(documentationConfiguration(restDocumentation)).build(); System.out.println("\n 规范初始化 .." +restDocumentation ); }
-
步骤定义代码:@Given("^create a rest document for VHR API$") public void create_a_rest_document_for_VHR_API() throws Throwable { RestAssured.given( spec ) .accept( "application/json" ) .filter( document( "vhrdocument" ) ) .when() .get( props.getVhrrequesturl() + "/vhrData/{vehicleID}", "5VW4T7AU0FM029999" ) .then().log().all(); }
-
请将 cmets 添加到您的问题中并正确格式化:)
-
了解您使用的 REST Docs 版本也很有用