Skip to content
/ jhttpmock Public

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

License

Notifications You must be signed in to change notification settings

voho/jhttpmock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages