CodeSVby CA Technologies

Lambda

< Back

Prerequisites

Java 8 functionality

CodeSV also supports lambda expressions from Java 8. They can be quite easy and conventionally used as matchers or with matchers.

Basic lambda example:

private static final String URL = "http://www.ca.com/portfolio?year=2016&tokenQuery=X4sPhj15WQE";

@Rule
public VirtualServerRule vs = new VirtualServerRule();



@Test
public void testLambda() throws Exception {
forGet(URL)
.matchesHeader("Custom-Header", s -> s.equals("CustomValue"))
.doReturn(
okMessage()
.withJsonBody("Success"));

HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(URL);
request.addHeader("Custom-Header", "CustomValue");

HttpResponse response = client.execute(request);

BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}

String body = result.toString();

assertEquals(200, response.getStatusLine().getStatusCode());
assertNotNull(body);
}

Complete example is here.