Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue on Quick Start: API Mocking with Java and JUnit 4 #307

Open
SiKing opened this issue Aug 16, 2024 · 1 comment
Open

Issue on Quick Start: API Mocking with Java and JUnit 4 #307

SiKing opened this issue Aug 16, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@SiKing
Copy link

SiKing commented Aug 16, 2024

Page

/docs/quickstart/java-junit/

Details

After copy/pasting the code. I get a compile error:
wiremockServer cannot be resolved

I tried to change wiremockServer.url("/my/resource") to URI.create(wireMockRule.url("/my/resource")), but then I got:
The method POST(HttpRequest.BodyPublisher) in the type HttpRequest.Builder is not applicable for the arguments ()

Suggested Edits

No response

References

No response

@SiKing SiKing added the bug Something isn't working label Aug 16, 2024
@SiKing
Copy link
Author

SiKing commented Aug 16, 2024

Eventually something like this worked for me:

    @Test
    public void exampleTest() throws Exception {
	// Setup the WireMock mapping stub for the test
	stubFor(get("/my/resource").withHeader("Content-Type", containing("xml"))
		.willReturn(ok().withHeader("Content-Type", "text/xml").withBody("<response>SUCCESS</response>")));

	// Setup HTTP **GET** request (with HTTP Client embedded in Java 11+)
	final HttpClient client = HttpClient.newBuilder().build();
	final HttpRequest request = HttpRequest.newBuilder().uri(URI.create(wireMockRule.url("/my/resource")))
		.header("Content-Type", "text/xml").GET().build();

	// Send the request and receive the response
	final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

	// Verify the response (with AssertJ)
	assertThat(response.statusCode()).as("Wrong response status code").isEqualTo(200);
	assertThat(response.body()).as("Wrong response body").contains("<response>SUCCESS</response>");
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant