Skip to content

Complex Assertions

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

Prerequisite

Verification of Invocation Count

This service is configured to respond to GET requests that are defined using the forGet(String url) method, and test check how many times the virtualized service is invoked.

For more complex asserting, you can also use advanced matchers such as the following:

  • moreThan()
  • moreThanOrEqualsTo()
  • lessThan()
  • lessThanOrEqualsTo()

Verification of invocation

public class VerificationExample {

  private static final String URL = "http://www.ca.com/portfolio";

  private static final String JSON_EXAMPLES_PORTFOLIO = "{"
      + "\"portfolio\": {\n"
      + "   \"id\": \"1\",\n"
      + "   \"year\": \"{{argument.year}}\",\n"
      + "  \"productNamesList\": [\n"
      + "    \"CA Server Automation\",\n"
      + "    \"CA Service Catalog\",\n"
      + "    \"CA Service Desk Manager\",\n"
      + "    \"CA Service Management\",\n"
      + "    \"CA Service Operations Insight\",\n"
      + "    \"CA Service Virtualization\"\n"
      + "  ]\n"
      + "}}";

  @Rule
  public VirtualServerRule vs = new VirtualServerRule();

  @Test
  public void testInvokedCount() throws Exception {
    forGet(URL)
        .matchesHeader("Accept-Language", contains("us"))
        .doReturn(
            okMessage()
                .withJsonBody(JSON_EXAMPLES_PORTFOLIO)
        ).invoked(2);

    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(URL);
    request.addHeader("Accept-Language", "en_us");

    client.execute(request);
    client.execute(request);
  }
}

For a complete example see: VerificationExample