0

Im using a mockserver to test api requests.

The mockserver wont start.

This is my code

@SpringBootTest
class RestApiMetaDataApplicationTests {
    private ClientAndServer mockServer;

    @BeforeClass
    public void startServer() throws Exception {
        mockServer = startClientAndServer(1080);
    }
 
    @AfterClass 
    public void stopServer() { 
        mockServer.stop();
        
    }
    
  
    @Test
    public void shouldReturnTrack() throws Exception {
        String isrc = "TEST1234560000";
        String responseBody = "{\"id\": \"1234\",\"name\": \"Some new track!\",\"duration_ms\": 12000000}";

        new MockServerClient("localhost", 1080)
            .when(
                request()
                    .withMethod("GET")
                    .withPath("/codechallenge/createTrack/" + isrc)
            )
            .respond(
            response()
                .withStatusCode(200)
                .withBody(responseBody)
                .withHeader("Content-Type", "application/json")
        );

    HttpClient httpClient = HttpClient.newHttpClient();
    HttpRequest httpRequest = HttpRequest.newBuilder()
        .GET()
        .uri(URI.create("http://localhost:1080/codechallenge/createTrack/" + isrc))
        .header("Content-Type", "application/json")
        .build();
    HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
    String response = httpResponse.body();

    assertEquals(responseBody, response);
    assertEquals(200, httpResponse.statusCode());
}}

There is nothing running on port 1080 and I have tried other ports and none of them work.

I have found this thread on Github of other people encountering the same issue but none of the solutions offered have worked for me

link to github

The dependencies I am using are

        <dependency>
        <groupId>org.mock-server</groupId>
        <artifactId>mockserver-netty</artifactId>
        <version>5.11.2</version>
    </dependency>
    <dependency>
        <groupId>org.mock-server</groupId>
        <artifactId>mockserver-client-java</artifactId>
        <version>5.11.2</version>
        <scope>test</scope>
    </dependency>

This is some of the stacktrace

org.mockserver.client.SocketConnectionException: Unable to connect to socket localhost/127.0.0.1:1080
at org.mockserver.client.NettyHttpClient.sendRequest(NettyHttpClient.java:180)
at org.mockserver.client.MockServerClient.sendRequest(MockServerClient.java:211)
at org.mockserver.client.MockServerClient.sendRequest(MockServerClient.java:244)
at org.mockserver.client.MockServerClient.upsert(MockServerClient.java:1109)
at org.mockserver.client.ForwardChainExpectation.respond(ForwardChainExpectation.java:50)
at net.codejava.song.RestApiMetaDataApplicationTests.shouldReturnTrack(RestApiMetaDataApplicationTests.java:64)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
at

added plugin import for mockserver

<plugin>
            <groupId>org.mock-server</groupId>
            <artifactId>mockserver-maven-plugin</artifactId>
            <version>3.10.8</version>
            <configuration>
                <serverPort>1080</serverPort>
                <proxyPort>1090</proxyPort>
                <logLevel>DEBUG</logLevel>
                <initializationClass>org.mockserver.maven.ExampleInitializationClass</initializationClass>
            </configuration>
            <executions>
                <execution>
                    <id>process-test-classes</id>
                    <phase>process-test-classes</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                </execution>
                <execution>
                    <id>verify</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

1 Answers1

0

the reason it is failing is you do not start the mock server and upon test, it will fail because there is nothing started at that port, I'm assuming you want it to automatically start server before the test, thus there are two options:

first: configure test task to be dependent of moctserver:run task second: when running test task before running a test task run a mock server.

check official docs for more info

Mohammed Fataka
  • 150
  • 1
  • 8