Thomas Sundberg

August 7, 2011

Test drive the implementation of a database layer

This post is available at


  1. Hi Thomas,

    I follow your posts with a lot of interest and I have a question for you (your experience could be helpful for me).

    I’m the (new) software architect of a J2EE application that uses EJB 2.1 (Entity Bean, Session Bean, and some MDB). This app is linked to a relational database and I’d like to unit test a remote method of a Session Bean which have dependency on the database (could perform some find, and some update).

    Do you have an idea on how I could “unit test” this kind of thing ? The aim is to integrate the test into a maven build as an integration-test (I supposed)…
    For sure, I cannot do INSERT SQL statement in the setup() method : the datamodel is too big for this kind of approach…

    Any idea, or help will be appreciated šŸ˜€


    Comment by BenoĆ®t — May 3, 2012 @ 18:59

    • Hi BenoĆ®t,

      You have a method that has a dependency to yet another class that may perform actions in a database. And you want to test the method. And just to spice the mix somewhat, this method is normally executed in a application container. How could this method be tested without the need for the application container and specially without the need for the database? My answer would be, it is difficult unless you are able to inject your own implementations of the dependencies. But if you can inject you own implementation, or use a mock, then it should be relative easy. You can’t assert the behavior by watching the changes in a database but you can assert that the method that will do things in the database is called properly.

      So, my approach would be to instantiate the class with the remote method in. Inject whatever dependencies it need. Call the method you really want to test and assert that the dependencies are called properly. This will require either setters for each dependency or that you can set them through a constructor. I would probably use Mockito to create the fake implementations of the dependencies so I can verify that the interaction has occurred properly.

      Verifying that the database changes are done correct is something I would try to avoid from a session bean. It would be better from another test that only verifies the database component, possible using an in memory database to speed up the execution.

      I hope this have given you some new ideas.

      Comment by Thomas Sundberg — May 7, 2012 @ 05:27

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: