unit testing data storage – Education Career Blog

Suppose I have an interface with methods ‘storeData(key, data)’ and ‘getData(key)’. How should I test a concrete implementation? Should I check if the data was correctly set in the storage medium (eg an sql database) or should I just check whether or not it gives the correct data back by using getData?

If I look up the data in the database it feels like I’m also testing the internals of the method but only checking whether it gives the same data back feels incomplete.


You seem to be caught up in the hype of unit testing, what you will be doing is actually an integration test. Setting and getting back the same value from the same key is a unit test you’d do with a mock implementation of the storage engine, but actually testing the real storage, say your database, as you should, that is no longer a unit test, but it is a fundamental part of testing, and it sounds like integration testing to me. Don’t use unit testing as your hammer, choose the right tools for the right job. Divide your testing into more layers.


What you want to do in a unit test is make sure that the method does the job that it is supposed to do. If the method uses dependencies to accomplish it’s work, you would mock those dependencies out and make sure that your method calls the methods on the objects it depends on with the appropriate arguments. This way you test your code in isolation.

One of the benefits to this is that it will drive the design of your code in a better direction. In order to use mocking, for example, you naturally gravitate towards more decoupled code using dependency injection. This gives you the ability to easily substitute your mock objects for the actual objects that your class depends on. You also end up implementing interfaces, which are more naturally mocked. Both of these things are good design patterns and will improve your code.

In order to test your particular example, for instance, you might have your class depend on a factory to create connections to the database and a builder to construct parameterized SQL commands that are executed via the connection. You’d pass these mocked versions of these objects to your class and ensure that the correct methods to set up the connection and command, build the correct command, execute it, and tear down the connection were invoked. Or perhaps, you inject an already open connection and simply build the command and invoke it. The point is your class is built against an interface or set of interfaces and you use mocking to supply objects that implement those interfaces and can record invocations and supply correct return values to the methods that you expect to use from the interface(s).


In cases like this I will usually create SetUp and TearDown methods that fire before/after my unit tests. These methods will set up any test data I need in the db and delete any test data when I’m done. Pseudo code example:

Const KEY1 = "somekey"
Const VALUE1= "somevalue"

Const KEY2 = "somekey2"
Const VALUE2= "somevalue2"

Sub SetUpUnitTests()
   Insert Into SQLTable(KEY1,VALUE1)

//this test is not dependent on the setData Method
Sub GetDataTest()

//this test is not dependent on getData Method
Sub SetDataTest()
   Assert.IsNotNull(Direct Call to SQL Select data from table where key=KEY2) 


Sub TearDownUnitTests()
   Delete From table Where key in (KEY1, KEY2)


Testing both in concert is a common technique (at least, in my experience), and I wouldn’t shy away from it. I’ve used this same pattern for serializing/deserializing and parsing and printing.

If you don’t want to hit the database, you could use a database mock. Some people have the same feelings as you when using mocks – it is partly implementation specific. As in all things, it’s a trade-off: consider the benefits of mocking (faster, not db dependent) vs its downsides (won’t detect actual db problems, slower).


I think it depends on what happens to the data later – if you’re only ever going to access the data using storeData and getData, why not test the methods in concert? I suppose there’s a chance that a bug will arise and it’ll be slightly harder to figure out whether it’s in storeData or getData, but I’d consider that an acceptable risk if it

  1. makes your test easier to implement, and
  2. conceals the internals, as you say

If the data will be read from, or inserted into, the database using some other mechanism, then I’d check the database using SQL as you suggest.

@brendan makes a good point, though – whichever method you decide on, you’ll be inserting data in the database. It’s a good idea to clear out the data before and after the tests to ensure that you can achieve consistent results.

Leave a Comment