Class MockRocketClient

  • All Implemented Interfaces:
    RocketClient

    public class MockRocketClient
    extends Object
    implements RocketClient
    Mock implementation of RocketClient for 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 error
     

    Verifying 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 Detail

      • MockRocketClient

        public MockRocketClient()
        Creates a new mock client instance.
    • 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 name
        value - 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 match
        latencyMs - 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 match
        statusCode - 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 method
        urlPattern - 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: RocketClient
        Executes an HTTP request based on the provided request specification.
        Specified by:
        execute in interface RocketClient
        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: RocketClient
        Sets the SSL context to be used for HTTPS requests.
        Specified by:
        configureSsl in interface RocketClient
        Parameters:
        sslContext - The SSL context to use.