I like writing tests. I think that is a well known fact. I even write tests for most of my “pet” projects. However, some of the most difficult tests to write are those querying data. The “get” repository functions or the “findBy” functions. Why are these functions so difficult to fully test? Because there are so many different permutations.
Let’s start with a simple example. A
findPersonByName(String firstName, String lastName) method. The SQL for this method would seem straightforward.
Obviously I will want to test this with a live database. So I will need to setup test data.
Then execute my method and verify it returned the row. “What’s so hard about that?” you might ask. My test data did not exercise the query. The same test method would pass if the SQL was written as so.
Why is that? Because there was not any negative data. There was only one record, so of course it would return the expected record. A more complete set of data would look like this.
Now there is negative data in the table that the original query should exclude. Notice I buried the expected record in the middle of the data. This helps guard against database ordering. If the expected record was inserted first, then the bogus “limit 1″ query could potentially still pass.
Now, what happens if the
findPersonByName(String, String) method should allow for wildcard searches? The sound you just heard was an unhandled exception in your brain. And this is for a simple “find” method with 2 parameters. Think about how many “find” methods you have in your codebase with how many different parameters.
I often find it very difficult to anticipate and produce all of the negative data. I simply try to cover as many as I can. Please comment if you find any cases that I should have covered.