2016-09-14 8 views
0

RestAssuredを使用するいくつかのJavaテストがあります。多くのテストでは、given()とwhen()のパラメータは異なりますが、then()セクションは同じで、複数のassertThat()文で構成されています。 then()ブロックを何度も繰り返し使用できる新しいメソッドに移動するにはどうすればよいですか?RestAssuredで共通のアサートを再利用する方法

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

} 
関連する問題