Menu Close

Testing a simple webapp

2 weeks ago we discussed build systems.  I showed how to create a simple webapp using an maven archetype.

It’s time to add a test.

Can we add tests to a simple webapp?

I’ve found a project called HtmlUnit.  Two weeks ago I suggested HttpUnit. Unfortunately, it looks abandoned.   It’s last release was 16 years ago.

https://github.com/HtmlUnit/htmlunit has the maven repo we need.

This week I’ve tried to integrate it into my build repo.  I had a weird time trying to get maven to compile my test.   After an hour or two, I discovered I had put it under src/test/com not src/test/java/com.  Conventions work well when you remember to follow them.

I got my test to compile and run. Afterwards, of course, it could not connect to localhost because nothing was listening to port 8080.

I cribbed from https://stackoverflow.com/questions/22203441/starting-server-for-integration-test-fails to get jetty running.

And Jetty didn’t run properly, and immediately returned a 500 error.  With lots of class not found stack traces .  I had a bit of fixing up to do.

Stack traces were due to a version issue.  Updating to the latest version of jetty and it ran and still returned a 500 error and never stopped.

My errors was using jetty:run to start it not jetty:start.

I made this change and got farther, but the test itself was failing.

My other error was setting the stop port and the connector port the same.  I had the web connector listening on port 8080 to all interfaces, and the stop port on 8080 on the 127.0.0.1 interface. Http connections from anywhere except localhost worked.  It was confusing until I realized my error.

You can find the final result at https://github.com/alesgaroth/build

Lessons learned:

  1. Follow the conventions for placing your source code when using a system like maven 
  2. Make sure you are using recent software.
  3.  jetty:start and jetty:run both start the webserver.  One lets you go on to test.  The other does not.  The documentation I found as I was trying to create it wasn’t particularly clear. I will say  Google has been getting worse at finding good, up-to-date documentation for software. 
  4.  Multiple ports on the same port number don’t work as nicely as you would like them to.