Skip to Content

A web server for your tests

Posted on 4 mins read

Let’s say you’re writing some code that makes http calls. And you’d like to write an integration test to secure your code.

How would you start a web server that serves only what you need for your test?

Wait… That’s easy, you’d use the web server I talked to you about yesterday!

import net.codestory.http.*;

public class IntegrationTest {
  public test() {
    new WebServer(routes -> routes.get("/uri", "Hello")).start();

    ... Connect to the server on port 8080

This piece of code starts an http server that answers Hello World when somebody sends a GET request on port 8080.

One line of code that's nice!

If you, like me, tend to run your tests in parallel, starting a web server on port 8080 will be a problem. You’d be tempted to find a free port using some kind of ServerSocket trick. But that’s not completely bulletproof. Just change your code this way and you’re done:

int thePort = new WebServer(routes -> routes
  .get("/uri", "Hello")).startOnRandomPort();

... Connect to the server on port 'thePort'

Under the hood, it chooses a random port, tries to bind it and if there’s a problem, retries with another port. That is bulletproof.

Ok, now what if you expect a json response and not a simple Hello String? Easy as pie bean.

new WebServer(routes -> routes
  .get("/uri", new MyBean())).startOnRandomPort();

MyBean is automatically serialised to json using Jackson.

Now, you have no excuse to not test your code that consumes content from a web server. Oh and I forgot to tell you that the server starts in less than 300ms, so you can use and abuse.

comments powered by Disqus