Skip to content

Post Call Verification

Roman Jakubco edited this page Jan 11, 2018 · 2 revisions

Prerequisite

CodeSV supports post execution verification. This makes it possible to check how many times a specific request was invoked. It is possible to check all possible HTTP methods using verify methods for example; verifyGet(String url). It is also possible to create complex verification's using matchers with which you can filter only specific request matching criteria.

Post Verification Example:

  private static final String BASE_URL = "http://www.ca.com/portfolio";
  private static final String QUERY = "?year=2016";
  private static final String URL = BASE_URL + "/{id}" + QUERY;


  @Rule
  public VirtualServerRule vs = new VirtualServerRule();

  @Test
  public void testPostVerification() throws IOException {
    HttpFluentInterface.forGet(URL)
        .matchesBody(matchesTemplate(""))
        .doReturn(
            okMessage()
              .withStringBody("Success")
        );

    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request1 = new HttpGet(BASE_URL + "/1" + QUERY);
    client.execute(request1);

    HttpClient client2 = HttpClientBuilder.create().build();
    HttpGet request2 = new HttpGet(BASE_URL + "/2" + QUERY);
    client2.execute(request2);
    client2.execute(request2);

    verifyGet(URL).invoked(3);
    verifyGet(BASE_URL + "/1").invoked(1);
    verifyGet(BASE_URL + "/2").invoked(2);
    verifyGet(URL).matchesQuery("year", "2016").invoked(3);
  }

For a complete example see: PostVerificationExample