lundi 16 mai 2016

Wiremock tests always getting a 404 on a simple request

I'm trying to get wiremock to return a 200 status with a simple request in my unit tests however, this unit test is always returning a 404 error.

How can this be solved?

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertTrue;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Rule;
import org.junit.Test;

import java.net.HttpURLConnection;
import java.net.URL;

public class WiremockTest {

@Rule
public WireMockRule wireMockRule = new WireMockRule(8089); // No-args constructor defaults to port 8080

@Test
public void exampleTest() throws Exception {
    stubFor(get(urlPathMatching("/my/resource[0-9]+"))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "text/xml")
                    .withBody("<response>Some content</response>")));

    int result = sendGet("http://localhost/my/resource/121");
    assertTrue(200 == result);

    //verify(getRequestedFor(urlMatching("/my/resource/[a-z0-9]+")));
}

private int sendGet(String url) throws Exception {
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");

    int responseCode = con.getResponseCode();
    return responseCode;

}
}
}

Aucun commentaire:

Enregistrer un commentaire