2017-11-21 11 views
0

스프링 오큐의 jvm을 사용하여 서비스 용 나머지 API 용 스프링 레스트 문서를 테스트하려고하지만 프레임 워크가 실행될 때 null 포인터 예외로 끝납니다. 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) 

코드 :

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

단계 정의 코드 :이 사용하기위한 것과 같이 JUnitRestDocumentation를 사용하지 않는

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

오류 메시지 : org.springframework.restdocs.JUnitRestDocumentation.beforeOperation (JUnitRestDocumentation.java에서 org.springframework.restdocs.ManualRestDocumentation.beforeOperation (ManualRestDocumentation.java:90) 에서 java.lang.NullPointerException이 : 76) – ravi

+0

@Autowired 개인 AppProperties 소품; @Before (@ "나머지") 공개 무효 beforeScenario() { JUnitRestDocumentation restDocumentation = 새로운 JUnitRestDocumentation ("타겟/생성-단편"); System.out.println ("jUnitRestDocumentation"+ restDocumentation); spec = new RequestSpecBuilder(). addFilter (documentationConfiguration (restDocumentation)) .build(); System.out.println ("\ n spec init .."+ restDocumentation); } – ravi

+0

단계 정의 코드 : @Given ("^ VHR API를 $의 나머지 문서 작성") 공공 무효 create_a_rest_document_for_VHR_API()의 Throwable를 throw를 { RestAssured.given (사양) .accept ("응용 프로그램/JSON") .filter (document ("vhrdocument")) .when() .get (props.getVhrrequesturl() + "/ vhrData/{vehicleID}", "5VW4T7AU0FM029999") .then(). log(). all(); } – ravi

답변

1

. 이 @Rule 주석 공공 필드해야 의미의 JUnit 규칙으로 사용할 수 있도록 설계 :

@Rule 
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(); 

는 규칙이 JUnit을 자동으로 봄 REST 문서가 설정할 수, 각 시험 restDocumentation를 호출된다는 것을 의미 인 테스트 관련 문맥을 무너 뜨립니다. restDocumentation이 이러한 방식으로 호출되지 않았으므로 NullPointerException이 발생하고 있으므로 컨텍스트가 설정되지 않았습니다. 당신은 오이를 사용하고 있지만, 그것의 사용하는 경우 어떻게 설명하지 않은

JUnit runner 당신은 위의 그림과 같이 @Rule -annotated 필드로 restDocumentation를 선언하여 문제를 해결할 수 있어야합니다. JUnit 러너를 사용하지 않는 경우 Spring REST Docs 'ManualRestDocumentation을 사용해야 할 수도 있습니다. Spring REST Docs 레퍼런스 문서는 JUnit을 사용하지 않을 때 테스트를 설정하는 방법을 설명하는 a section을 포함하고있다.

1

JUnitRestDocumentation 인스턴스를 선언 한 클래스를 상속하는 테스트 클래스가 여러 개 있기 때문에 동일한 문제가 발생했습니다. 내 실수는 @Rule 주석을 사용하여 규칙을 선언 한 것이 었습니다. @ClassRule을 사용하고 인스턴스를 static으로 선언해야합니다.

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