2016-09-14 2 views
0

RestAssured를 사용하는 Java 테스트가 있습니다. 많은 테스트에서 given() 및 when() 매개 변수는 다르지만 then() 섹션은 동일하며 여러 assertThat() 문으로 구성됩니다. then() 블록을 계속해서 사용할 수있는 새로운 메서드로 이동하려면 어떻게해야합니까?RestAssured와 함께 일반 assert를 재사용하는 방법

@Test 
public void test_inAppMsgEmptyResponse() { 
    given(). 
      contentType("application/json"). 
    when(). 
      get("inapp/messages.json"). 
    then().assertThat(). 
      statusCode(HttpStatus.SC_OK). 
      assertThat(). 
      body("pollInterval", equalTo(defaultPollInterval)). 
      assertThat(). 
      body("notifications", hasSize(0)); 
} 

답변

0

여러 응답에 사용할 수있는 주장의 집합을 만들 수 ResponseSpecification를 사용할 수 있습니다. 이 질문은 귀하가 질문하는 방식과 조금 다르지만 귀하의 필요를 처리 할 수 ​​있습니다. 이 예에서는 RequestSpecification을 사용하여 여러 개의 Rest 통화 전체에서 사용할 수있는 공통 요청 설정을 설정합니다. 이것은 완전히 테스트되지는 않았지만 코드는 다음과 같습니다.

public static RequestSpecBuilder reqBuilder; 
public static RequestSpecification requestSpec; //set of parameters that will be used on multiple requests 
public static ResponseSpecBuilder resBuilder; 
public static ResponseSpecification responseSpec; //set of assertions that will be tested on multiple responses 

@BeforeClass 
public static void setupRequest() 
{ 
    reqBuilder = new RequestSpecBuilder(); 
    //Here are all the common settings that will be used on the requests 
    reqBuilder.setContentType("application/json"); 
    requestSpec = reqBuilder.build(); 
} 

@BeforeClass 
public static void setupExpectedResponse() 
{ 
    resBuilder = new ResponseSpecBuilder(); 
    resBuilder.expectStatusCode(HttpStatus.SC_OK) 
    .body("pollInterval", equalTo(defaultPollInterval)) 
    .body("notifications", hasSize(0)); 
    responseSpec = resBuilder.build(); 
} 


@Test 
public void restAssuredTestUsingSpecification() { 
    //Setup the call to get the bearer token 
    Response accessResponse = given() 
     .spec(requestSpec) 
    .when() 
     .get("inapp/messages.json") 
    .then() 
     //Verify that we got the results we expected 
     .spec(responseSpec); 

} 
관련 문제