WebCalc iteration 1 - setting up with one test

Welcome to the first iteration of WebCalc implementation. In my previous post I just explained my motivations and goals for this project. This time I’ll do some actual coding. Although in the end, I’ll try to have something “business” related, I guess most of the time I’ll spend wiring things up. To the keyboard then.

Goal

I want to have a very first and simple test working: adding 1 to 2. It will go to the REST interface so that it would force me to wire up everything to get an application available over HTTP/2. This would clearly be an integration test. Below that, I’ll have my unit tests.

A bit of infrastructure

The easiest way to start a Spring Boot project is to visit Spring Initializr. I chose a Gradle project with the newest version of Spring Boot, Java 10 and Reactive Web. This should get me started simple. See commit fb831f9.

One thing, that I need to fix almost immediately, was removing JUnit 4 and plugging in JUnit 5. Nothing difficult, but it would be nicer to have a boot started with JUnit 5 already. So build.gradle changes:

Now I need to change the test because it’s using old infrastructure.

First test

Now I can write my first test. As I don’t have anything so far, I need to start completely from outside, so through my REST interface (an idea of a walking skeleton). I also need to decide how this interface would look like. Let’s say, that my endpoint will be responding to GET requests on URL “/eval”. Let’s also say, that, to keep it simple, I’ll not be using JSON, XML or anything, I’ll be using good old “text/plain”. Just for the fun of it, and to avoid weird parsing issues, I’ll be expecting my input to be using reverse Polish notation. Last decision: to actually test my application from outside I’ll be using REST Assured.

OK, from now on, there should be more coding and less talking. First, I need to import REST Assured:

I can finally write my first test:

It fails miserably, as expected. After reading Spring documentation a bit I can see, that using WebFlux is probably overkill, so I’m going back and replacing it with good old SpringMVC. As the most basic things like controllers are common to both stacks, if I were to switch later on to WebFlux, it would probably take me very little time.

If I take a look at test results, the problem there is, there’s nobody listening on localhost:8080.

java.net.ConnectException: Connection refused (Connection refused)

As the application actually didn’t even start, it’s not a wonder. Let’s fix that.

The application was started, it listens on port 8080, but nobody responds to request to “/eval”.

java.lang.AssertionError: 1 expectation failed.
Response body doesn't match expectation.
Expected: "3"
  Actual: {"timestamp":"2018-09-04T06:44:17.247+0000","status":404,
     "error":"Not Found","message":"No message available","path":"/eval"}

I need a controller.

Now it looks far better, I got an answer, but not the correct one. That’s easy.

Tada! First test green.

That’s it this time, next time I’ll go deeper into the application and separate web part from calculations. See you soon!