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.
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();
}
}
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>
- added alternatives (e.g. simulating errors with a certain probability)
- updated readme
- initial release