Channels ▼
RSS

Database

Coarse-Grained Unit Testing



In an ideal world, a unit test should only test a small piece of code, should be repeatable, and be independent of external dependencies such as application servers and databases. However, because of legacy code constraints and budgeting concerns (among other reasons), it's not always possible to write ideal tests. Code that depends on databases can be especially difficult to test because of the time it takes to mock and test. On the other hand, if real database code is used in the test, it is often difficult to make test runs repeatable due to the nature of real-world data.

Writing coarse-grained tests is an option. One benefit of coarse-grained tests is that they can test the integration points of dependant components. For instance, Java-based enterprise applications typically use Enterprise Java Bean (EJB) technology. Most EJB components depend either directly or indirectly on the database to complete processing. Such components can be difficult to test because it takes time to write, build, deploy, and see verify that the code works correctly. In this article I show how you can write coarse-grained repeatable unit tests for such components using OpenEJB and DBUnit. The complete source code and related files are available The complete source code and related files are available here.

An Example

To examine coarse-grained testing, I use the example of a spaceship ticket booking application. Blue Star Galactic is a small spaceship company that transports people from one place to another place within our galaxy. Being a transportation company, it requires ticket booking software. The requirements for the software are straightforward: Since the spaceships are small, they can only transport five passengers at a time. Consequently, the booking engine should limit five ticket bookings for the same destination. And since only one person can travel with the same name from one place to another, the application should not allow the same passenger to book more than one ticket in the same name for the same source and destination.

All the tickets need to get stored somewhere. I chose MySQL database since it is open source and straightforward to setup. You can use any database you want with few minor changes in JDBC properties. After installing MySQL, I created a database called "spaceship". Then I created a table called "Ticket" to store the ticket bookings with columns as in Figure 1.

Figure 1

That's all the app requires from the database setup side. Scripts to create the database and table are also included in the project under db-scripts folder (available here.). While this app is simple, it scales to larger, more complex projects. Finally, I use the Eclipse IDE but without installing any additional plug-ins. Figure 2 depicts the key components of the application.

Figure 2


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video