Package com.guinetik.rr.http
Class MockRocketClient
- java.lang.Object
-
- com.guinetik.rr.http.MockRocketClient
-
- All Implemented Interfaces:
RocketClient
public class MockRocketClient extends Object implements RocketClient
Mock implementation ofRocketClientfor unit testing without network requests.This client simulates HTTP responses based on predefined rules, enabling unit testing of code that depends on RocketRest without requiring actual network connectivity. It supports URL pattern matching, response simulation, and invocation tracking.
Features
- Predefined mock responses for method/URL combinations
- Regex-based URL pattern matching
- Simulated network latency
- Custom HTTP status codes
- Invocation counting for verification
Basic Usage
MockRocketClient mockClient = new MockRocketClient(); // Add mock response mockClient.addMockResponse("GET", "/users/.*", (url, body) -> { User user = new User(); user.setId(1); user.setName("Test User"); return user; }); // Execute request - returns mock response RequestSpec<Void, User> request = RequestBuilder.<Void, User>get("/users/1") .responseType(User.class) .build(); User user = mockClient.execute(request);Simulating Latency and Status Codes
mockClient.withLatency("/slow/.*", 1000L); // 1 second delay mockClient.withStatusCode("/error/.*", 500); // Server errorVerifying Calls
// Check invocation count int count = mockClient.getInvocationCount("GET", "/users/.*"); assertEquals(1, count); // Reset for next test mockClient.resetCounts();- Since:
- 1.0.0
- Author:
- guinetik <guinetik@gmail.com>
- See Also:
RocketClient,RocketRestMock
-
-
Constructor Summary
Constructors Constructor Description MockRocketClient()Creates a new mock client instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MockRocketClientaddMockResponse(String method, String urlPattern, BiFunction<String,Object,Object> responseProducer)Adds a mock response for a specific HTTP method and URL pattern.voidconfigureSsl(SSLContext sslContext)Sets the SSL context to be used for HTTPS requests.<Req,Res>
Resexecute(RequestSpec<Req,Res> requestSpec)Executes an HTTP request based on the provided request specification.intgetInvocationCount(String method, String urlPattern)Gets the number of times a specific endpoint has been invokedvoidresetCounts()Resets all invocation countsvoidsetBaseUrl(String baseUrl)MockRocketClientwithHeader(String name, String value)Sets a custom header value that will be included in response dataMockRocketClientwithLatency(String urlPattern, long latencyMs)Sets the latency for a specific endpoint in millisecondsMockRocketClientwithStatusCode(String urlPattern, int statusCode)Sets the status code for a specific endpoint
-
-
-
Method Detail
-
withHeader
public MockRocketClient withHeader(String name, String value)
Sets a custom header value that will be included in response data- Parameters:
name- Header namevalue- Header value- Returns:
- This MockRocketClient instance for chaining
-
withLatency
public MockRocketClient withLatency(String urlPattern, long latencyMs)
Sets the latency for a specific endpoint in milliseconds- Parameters:
urlPattern- URL pattern to matchlatencyMs- Latency in milliseconds- Returns:
- This MockRocketClient instance for chaining
-
withStatusCode
public MockRocketClient withStatusCode(String urlPattern, int statusCode)
Sets the status code for a specific endpoint- Parameters:
urlPattern- URL pattern to matchstatusCode- HTTP status code- Returns:
- This MockRocketClient instance for chaining
-
addMockResponse
public MockRocketClient addMockResponse(String method, String urlPattern, BiFunction<String,Object,Object> responseProducer)
Adds a mock response for a specific HTTP method and URL pattern. The URL pattern is treated as a regex pattern for more flexible matching.- Parameters:
method- HTTP method (GET, POST, PUT, DELETE)urlPattern- URL pattern to match (regex supported)responseProducer- Function that takes (url, requestBody) and returns a response object
-
getInvocationCount
public int getInvocationCount(String method, String urlPattern)
Gets the number of times a specific endpoint has been invoked- Parameters:
method- HTTP methodurlPattern- URL pattern- Returns:
- Number of invocations
-
resetCounts
public void resetCounts()
Resets all invocation counts
-
execute
public <Req,Res> Res execute(RequestSpec<Req,Res> requestSpec) throws RocketRestException
Description copied from interface:RocketClientExecutes an HTTP request based on the provided request specification.- Specified by:
executein interfaceRocketClient- Type Parameters:
Req- The type of the request body.Res- The type of the response.- Parameters:
requestSpec- The specification of the request to be executed.- Returns:
- The response object.
- Throws:
RocketRestException- If an error occurs during the request execution.
-
configureSsl
public void configureSsl(SSLContext sslContext)
Description copied from interface:RocketClientSets the SSL context to be used for HTTPS requests.- Specified by:
configureSslin interfaceRocketClient- Parameters:
sslContext- The SSL context to use.
-
setBaseUrl
public void setBaseUrl(String baseUrl)
- Specified by:
setBaseUrlin interfaceRocketClient
-
-