JVM | Platform | Status |
---|---|---|
OpenJDK (Temurin) Current | Linux | |
OpenJDK (Temurin) LTS | Linux | |
OpenJDK (Temurin) Current | Windows | |
OpenJDK (Temurin) LTS | Windows |
A tiny embedded HTTP server for unit testing.
- An embedded HTTP server for simulating external services.
- Conveniently enqueue responses to arbitrary requests.
- Verify that requests were received as expected.
- Zero dependencies.
- Written in pure Java 17.
- OSGi ready.
- JPMS ready.
- ISC license.
- High-coverage automated test suite.
Any code that makes requests to an external service should be tested to
ensure that both the requests it makes are correct, and that it behaves
correctly when presented with various responses. The quixote
package
provides a tiny embedded web server that can be configured to return canned
responses to specified requests.
$ mvn clean verify
Create a QWebServer
before each test. The server will listen on the
specified port.
@BeforeEach
public void setup()
throws IOException
{
this.server =
QWebServers.createServer(42000);
this.http =
HttpClient.newHttpClient();
}
Enqueue responses to requests:
this.server.addResponse()
.forMethod("GET")
.forPath("/xyz")
.withContentType("text/plain")
.withFixedText("Hello 0.")
.withStatus(201)
.withHeader("Header-0", "XYZ");
Have code make requests to the server during tests, and then verify that the server received the requests:
final var requests =
new LinkedList<>(this.server.requestsReceived());
{
final var req = requests.remove(0);
assertEquals("GET", req.method());
assertEquals("/xyz", req.path());
}
Remember to clean up the server after each test:
@AfterEach
public void tearDown()
throws IOException
{
this.server.close();
}