One of the first things you'll notice when you start testing your web application with Selenium WebDriver is that there is no API to get the HTTP status code for a page. If you want to know why, you can go and read WebDriver issue #141. Personally I don't care why - I just want to test my HTTP reponse codes (especially 403 Permission Denied).
There is a fairly simple workaround you can use for WebDriver, but firstly lets look at how to do it without WebDriver. Nobody said you HAVE to use Selenium right?
Each of my unit tests extends from a different base class depending on what layer of my application they are testing. i.e.
BaseTest `- BaseDBTest `- BaseOpsTest `- BaseUITest
These superclasses are very convenient places to put shared test code like starting a server, opening the database, doing a HTTP get or even just checking that assert is enabled.
When testing the whole application, it doesn't make much sense to me to run tests from all layers in random order. There are clear dependencies between the layers and if a lower layer fails I want to know about it before it blows up into the upper layers (if you believe its better to mock unit test dependencies then you might as well stop reading now).
I've discovered that one reason I don't write as many unit tests as I should is because they live in a separate source tree, out of sight and out of mind. So I've moved my unit tests to be inner classes on the classes they test. A side effect of this is you can easily test the private methods of a class since the inner class has direct access to these methods.
Here is the configuration you will need to do this. Note, I am using TestNG, but it should be pretty similar in JUnit.
Why would you start at the bottom of this chart? People seem to do it all the time:
hmmmm... that seems like a good idea. i can whip it up in no time. okay, i guess i should test this. hey it did something! but, hang on i didn't really want it to do that. if i just change this i can get it to do something else. ouch! now the first bit broke, but i might still be able use it to if these conditions hold true. that's still useful isn't it? it could happen right? wait. hang on, what the hell are we trying to do again?
Repeat ad nauseum.
I'm fairly new to maven, but so far it has really impressed me with its simplicity, productivity gains and IDE independence. So I was really looking forward to porting some unit tests to TestNG, running mvn test and watching in awe as maven found my tests, ran them and generated a pretty html report of the test results. Unfortunately, it didn't turn out quite like that.