// <dependency> // <groupId>org.mock-server</groupId> // <artifactId>mockserver-netty</artifactId> // <version>5.12.0</version> // <scope>test</scope> // </dependency> import org.mockserver.client.MockServerClient; import org.mockserver.integration.ClientAndServer; public class NetworkSimulationExample { public static void main(String[] args) { ClientAndServer mockServer = ClientAndServer.startClientAndServer(1080); new MockServerClient("localhost", 1080) .when( request() .withMethod("GET") .withPath("/api/users") ) .respond( response() .withStatusCode(200) .withHeader("Content-Type", "application/json") .withBody("{ \"id\": 1, \"name\": \"John Doe\" }") ); HttpClient httpClient = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("http://localhost:1080/api/users")) .build(); HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); Assertions.assertEquals(200, response.statusCode()); Assertions.assertEquals("{ \"id\": 1, \"name\": \"John Doe\" }", response.body()); mockServer.stop(); } }


上一篇:
下一篇:
切换中文