voho / jhttpmock

Mock HTTP Server for Java (especially: unit + component tests)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jHTTPMock

Travis codecov.io JitPack Codacy Badge

Very simple mock HTTP Server for Java built with Jetty and JUnit. Inspired by WireMock and Jadler.

Key features:

  • fast onboarding
  • fluent API
  • latency and failure simulation
  • minimal dependencies, small footprint
  • supports multiple concurrent instances

On the contrary, there is no complex response modelling. Why not? I think the test cases should be as simple as possible and it should not be necessary to implement any complex logic.

Quick Example

The example is using JHttpMock as a JUnit rule.

public class BasicUseCase {
    @Rule
    public MockHttpServerRule mock = new MockHttpServerRule(new JettyMockHttpServer(8081));
    
    @Test
    public void test() {
        // define mock request behaviour
        
        mock
            .onRequest()
            .withUrlEqualTo("/ping")
            .thenRespond()
            .withCode(200)
            .withRandomDelay(Duration.ofMillis(30), Duration.ofMillis(50))
            .withBody("Hello!")
            .orRespondWithProbability(0.01)
            .withCode(503)
            .withFixedDelay(Duration.ofSeconds(5));
           
        // TODO: send HTTP request
        
        // verify mock server interactions
        
        mock
            .verifyThatRequest()
            .withUrlEqualTo("/ping")
            .wasReceivedOnce();
    }
}

Usage

Add this to your pom.xml file:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependency>
    <groupId>com.github.voho</groupId>
    <artifactId>jhttpmock</artifactId>
    <version>0.9-RC1</version>
</dependency>

Release Notes

1.0 (not yet released)

  • added alternatives (e.g. simulating errors with a certain probability)
  • updated readme
  • initial release

About

Mock HTTP Server for Java (especially: unit + component tests)

License:Apache License 2.0


Languages

Language:Java 100.0%