Unit testing Java Spark microframework
<p>I am trying to add a restful api to a java microservice. For this, I am using spark:</p>

<p><a href="http://sparkjava.com/documentation.html" rel="noreferrer">http://sparkjava.com/documentation.html</a></p>

<p>I've created a very simple class which stands up an api. That class is here:</p>

<pre><code>public class Routes {
    public void establishRoutes(){
        get("/test", (req, res) -&gt; "Hello World");
        after((req, res) -&gt; {
            res.type("application/json");
        });

        exception(IllegalArgumentException.class, (e, req, res) -&gt; {
            res.status(400);
        });
    }
</code></pre>

<p>Now, running Routes.establishRoutes() should stand up an api which would show "Hello World" in the event someone decides to visit <a href="http://localhost:4567/test" rel="noreferrer">http://localhost:4567/test</a>. This does actually work. Hurray!</p>

<p>The next step is unit testing the code. My unit test, unfortunately, does not succeed. The spark documentation does not detail a sound way for doing testing so what I have is pieced together from examples I found around the net. Here is my Junit test:</p>

<pre><code>public class TestRoutes {
        @Before
        public void setUp() throws Exception {
            Routes newRoutes = new Routes();
            newRoutes.establishRoutes();
        }

        @After
        public void tearDown() throws Exception {
            stop();
        }

        @Test
        public void testModelObjectsPOST(){

            String testUrl = "/test";


            ApiTestUtils.TestResponse res = ApiTestUtils.request("GET", testUrl, null);
            Map&lt;String, String&gt; json = res.json();
            assertEquals(201, res.status);
        }
</code></pre>

<p>Here is the code behind ApiTestUtils.request():</p>

<pre><code>public class ApiTestUtils {
    public static TestResponse request(String method, String path, String requestBody) {


        try {
            URL url = new URL("http://localhost:4567" + path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(method);
            connection.setDoOutput(true);
            connection.connect();
            String body = IOUtils.toString(connection.getInputStream());
            return new TestResponse(connection.getResponseCode(), body);
        } catch (IOException e) {
            e.printStackTrace();
            fail("Sending request failed: " + e.getMessage());
            return null;
        }
    }

    public static class TestResponse {

        public final String body;
        public final int status;

        public TestResponse(int status, String body) {
            this.status = status;
            this.body = body;
        }

        public Map&lt;String,String&gt; json() {
            return new Gson().fromJson(body, HashMap.class);
        }
    }
}
</code></pre>

<p>I am failing on <code>connection.connect()</code> inside <code>ApiTestUtils.request()</code>. Specifically, I get the error: <code>java.lang.AssertionError: Sending request failed: Connection refused</code></p>

<p>I believe this is happening because the application isn't listening when my test tries to make the request. However, I don't understand why that would be the case. I borrowed the test code from the demo project found here:
<a href="https://github.com/mscharhag/blog-examples/blob/master/sparkdemo/src/test/java/com/mscharhag/sparkdemo/UserControllerIntegrationTest.java" rel="noreferrer">https://github.com/mscharhag/blog-examples/blob/master/sparkdemo/src/test/java/com/mscharhag/sparkdemo/UserControllerIntegrationTest.java</a></p>

<p><strong>UPDATE:</strong> 
I tried running the example linked above. Turns out, it doesn't work either. Looks like spinning up a spark instance in this context is more difficult than I thought? I'm not trying to figure out how to do so.</p>