0

I'm trying to start simple test with mock-server. I use dependency:

testCompile group: 'org.mock-server', name: 'mockserver-netty', version: '5.6.1'

I have taken test example from the guide. My test is very simple:

public class MockServerTest {

private static ClientAndServer mockServer;

@BeforeClass
public static void startServer() {
    mockServer = startClientAndServer(1080);
}

private void simple() {
    new MockServerClient("127.0.0.1", 1080)
            .when(
                    request()
                            .withMethod("GET")
                            .withPath("/do"),
                    exactly(1)
            )
            .respond(
                    response()
                    .withStatusCode(200)
                            .withHeaders(
                                    new Header("Content-Type", "application/json; charset=utf-8"),
                                    new Header("Cache-Control", "public, max-age=86400"),
                                    new Header("Set-Cookie","SESSION=Njg1Mjg4NWQtZjRhOS00MDZhLWJhOGQtZmFmNWIzZjRmYTI4; Max-Age=1800; Expires=Wed, 4 Sep 2019 17:09:58 +0400; Path=/; HttpOnly; SameSite=Lax\n")
                            )

            .withDelay(TimeUnit.SECONDS,1)
            );
}

private org.apache.http.HttpResponse hitTheServerWithGetRequest(String page) {
    String url = "http://127.0.0.1:1080/"+page;
    HttpClient client = HttpClientBuilder.create().build();
    org.apache.http.HttpResponse response=null;
    HttpGet get = new HttpGet(url);
    try {
        response=client.execute(get);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return response;
}

@Test
public void simpleTest() {
    simple();
    HttpResponse resp = hitTheServerWithGetRequest("do");

    new MockServerClient("localhost", 1080).verify(
            request()
                    .withMethod("GET")
                    .withPath("/do"),
            VerificationTimes.exactly(1)
    );
}

 @AfterClass
public static void stopServer() {
    mockServer.stop();
}
}

But this test doesn't work. It starts but never finishes. After I start the test I can do requests with Postman, and I see logs in the console and obtain response - Not found (but must be Ok with specified headers). What is wrong with my code o with mockserver-netty?

Valeriy K.
  • 2,616
  • 1
  • 30
  • 53
  • What is `startClientAndServer(1080)`? Also, why do you mock the URL with `127.0.0.1` and then verify the call with `localhost`? Have you checked that MockServer can translate that to the IP? – Tom Sep 05 '19 at 07:44
  • I have taken code from here https://github.com/eugenp/tutorials/blob/master/testing-modules/mockserver/src/test/java/com/baeldung/mock/server/MockServerLiveTest.java . startClientAndServer is the library function. 127.0.0.1 or localhost doesn't matter - not works in both cases. – Valeriy K. Sep 05 '19 at 08:03

1 Answers1

0

Try checking on your response headers the expected response headers should match otherwise its going to return error 404

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 23 '22 at 12:41