Wiremock Standalone Verification not working....

574 views
Skip to first unread message

Pettedson John

unread,
Jul 28, 2020, 5:25:30 PM7/28/20
to wiremock-user

I have seen this issue mentioned in Stack overflow but no final response....

I am trying Wiremock running it as a standalone Server but I am having issues with the verification bit.

Here is a copy of my test.

package uk.gov.hmcts.futurehearings.hmi.integration.hearing;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static net.serenitybdd.rest.SerenityRest.expect;
import static org.junit.Assert.assertEquals;
import static uk.gov.hmcts.futurehearings.hmi.integration.common.TestingUtils.readFileContents;

import uk.gov.hmcts.futurehearings.hmi.Application;

import java.io.IOException;
import java.util.Map;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import lombok.extern.slf4j.Slf4j;
import net.serenitybdd.junit.spring.integration.SpringIntegrationSerenityRunner;
import net.serenitybdd.rest.SerenityRest;
import net.thucydides.core.annotations.Narrative;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@slf4j
@RunWith(SpringIntegrationSerenityRunner.class)
@Narrative(text={"In order to test that the Hearing Service is working properly",
"As a tester",
"I want to be able to execute the tests for various endpoints"})
@SpringBootTest(classes = {Application.class})
@activeprofiles("integration")
public class API_WiremockStandaloneTest {

private static final String INPUT_FILE_PATH = "uk/gov/hmcts/futurehearings/hmi/integration/hearing/input"; //WireMock wireMock = new WireMock("localhost", 9050); WireMockServer wireMockServer = new WireMockServer(9050); @Value("${employeeBaseURI}") private String employeeBaseURI; @Value("${employeeAPIPath}") private String employeeAPIPath; @Value("${timeAPIPath}") private String timeAPIPath; @Value("${hmctsAPIPath}") private String hmctsAPIPath; @Value("${serenity.outputDirectory}") private String serenityOutputDirectory; @Before public void initialiseValues() { RestAssured.baseURI = employeeBaseURI; SerenityRest.useRelaxedHTTPSValidation(); } @Test public void should_work_from_standalone_mock() throws Exception { System.out.println("The value of the base URI"+RestAssured.baseURI ); Response response = expect().that().statusCode(200) .given().contentType("application/json") //.body(readFileContents(INPUT_FILE_PATH + "/employee-demo-request.json")) //.headers(headersAsMap) //.baseUri("http://localhost:4550") .basePath("/employee") .queryParam("id","3") .when().get().then().extract().response(); wireMockServer.verify(1, getRequestedFor(urlEqualTo("/product/p0001"))); // When //LocalTime time = response.getTime("cet"); System.out.println(response.getBody().asString()); assertEquals(4,response.getBody().jsonPath().getMap("$").size()); Map<String, String> responseMap = response.getBody().jsonPath().getMap("$"); assertEquals("Tuesday",responseMap.get(("firstName"))); }

/* @after
public void reset() {
WireMock.reset();
}*/
}

My Mock End point.

http://localhost:9050/product/p0001
{
id: "1",
currentDateTime: "2019-03-12T10:54+01:00",
utcOffset: "01:00:00",
isDayLightSavingsTime: false,
dayOfTheWeek: "Tuesday",
timeZoneName: "Central Europe Standard Time FROM HMCTS",
currentFileTime: "131968616698822965",
ordinalDate: "2019-71",
serviceResponse: null
}

Am getting the error....

Expected at least one request matching: {
"url" : "/product/p0001",
"method" : "GET"
}
Requests received: [ ]
com.github.tomakehurst.wiremock.client.VerificationException: Expected at least one request matching: {
"url" : "/product/p0001",
"method" : "GET"
}
Requests received: [ ]
at com.github.tomakehurst.wiremock.client.WireMock.verificationExceptionForNearMisses(WireMock.java:551)
at com.github.tomakehurst.wiremock.client.WireMock.verifyThat(WireMock.java:538)
at com.github.tomakehurst.wiremock.client.WireMock.verifyThat(WireMock.java:521)
at com.github.tomakehurst.wiremock.WireMockServer.verify(WireMockServer.java:271)
at uk.gov.hmcts.futurehearings.hmi.integration.hearing.HearingAPI_WiremockStandaloneTest.should_work_from_standalone_mock(HearingAPI_WiremockStandaloneTest.java:92)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at net.serenitybdd.junit.runners.SerenityStatement.evaluate(SerenityStatement.java:33)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at net.serenitybdd.junit.runners.SerenityRunner.performRunChild(SerenityRunner.java:464)

Without the verification step the test passes.....

Any idea how I can make this work please?????

Tom Akehurst

unread,
Jul 28, 2020, 5:28:01 PM7/28/20
to wiremock-user
That's quite hard to to read. Could you re-post the code with formatting/colouring preserved?

Pettedson John

unread,
Jul 29, 2020, 4:52:40 AM7/29/20
to wiremock-user
Apologies didn't notice that the formatting was lost.

Here is the code again...

@Slf4j

@RunWith(SpringIntegrationSerenityRunner.class)
@Narrative(text={"In order to test that the Hearing Service is working properly",
"As a tester",
"I want to be able to execute the tests for various endpoints"})
@SpringBootTest(classes = {Application.class})
@ActiveProfiles("integration")
public class HearingAPI_WiremockStandaloneTest {


private static final String INPUT_FILE_PATH = "uk/gov/hmcts/futurehearings/hmi/integration/hearing/input";

//WireMock wireMock = new WireMock("localhost", 9050);
WireMockServer wireMockServer = new WireMockServer(9050);

@Value("${employeeBaseURI}")
private String employeeBaseURI;

@Value("${employeeAPIPath}")
private String employeeAPIPath;

@Value("${timeAPIPath}")
private String timeAPIPath;

@Value("${hmctsAPIPath}")
private String hmctsAPIPath;

@Value("${serenity.outputDirectory}")
private String serenityOutputDirectory;

@Before
public void initialiseValues() {

/* headersAsMap.put("Host", targetHost);
headersAsMap.put("Ocp-Apim-Subscription-Key", targetSubscriptionKey);
headersAsMap.put("Ocp-Apim-Trace", "true");
headersAsMap.put("Company-Name", "HMCTS");*/

RestAssured.baseURI = employeeBaseURI;
SerenityRest.useRelaxedHTTPSValidation();

}

@Test
public void should_work_from_standalone_mock() throws Exception {


System.out.println("The value of the base URI"+RestAssured.baseURI );

Response response = expect().that().statusCode(200)
.given().contentType("application/json")
//.body(readFileContents(INPUT_FILE_PATH + "/employee-demo-request.json"))
//.headers(headersAsMap)
//.baseUri("http://localhost:4550")
.basePath("/employee")
.queryParam("id","3")
.when().get().then().extract().response();


wireMockServer.verify(1, getRequestedFor(urlEqualTo("/product/p0001")));

// When
//LocalTime time = response.getTime("cet");
System.out.println(response.getBody().asString());
assertEquals(4,response.getBody().jsonPath().getMap("$").size());
Map<String, String> responseMap = response.getBody().jsonPath().getMap("$");
assertEquals("Tuesday",responseMap.get(("firstName")));

}

/* @After
Many thanks....
Reply all
Reply to author
Forward
0 new messages