Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write a test for a method that accesses a database such as following.

public class MyClass{
    public String getAddress(Int id){
        String query = "Select * from Address where id="+id;
        //some additional statements           
        resultSet = statement.executeQuery();
        return result.getString(ADDRESS);        

How can I test this method? I am using Java.

share|improve this question

closed as not a real question by gnat, Walter, GlenH7, MainMa, Giorgio Nov 18 '12 at 19:54

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

what have you tried? – gnat Nov 16 '12 at 18:03
@gnat I am new to java... I have just started learning junit and it seems that I have a lot of methods similar to ones I showed above. So I just wanted to know what I can use to test such a method because I was unable to do it using junit. – javaStudent Nov 16 '12 at 18:06
DBUnit is a JUnit extension that might help you. – Mike Partridge Nov 16 '12 at 18:16
I actually asked this today on SO -… . – m3th0dman Nov 16 '12 at 19:49
up vote 1 down vote accepted

You would probably be better served to skip the integration testing for most of these methods. Instead, you would want to look into mocks and stubs (Mockito is a well-regarded mocking framework for Java) and verifying that the method behaves in the proper way. Namely, building the expected SQL for the input (on a sidenote, you should be using parameterized queries, not just concatenating strings), passing it to the correct object, and returning the correct value from the mocked up row.

share|improve this answer

Remember that you aren't testing whether your database works.

If you are using a database from one of the top vendors (or even the second and third tiers) you can guarantee that they have tested that a SQL statement will read or write.

You probably want to test a number of things:

  • Did I get my connection properties right?
  • Do the columns returned by my SQL statement match my data structure?


Some of these can be addressed by mocking frameworks (as described by another poster). Some of these might be setup as integration tests that run independently of unit tests. Some of these can be tested using an in-memory database.

I've used HSQLDB in the past to create an inmemory database, pre-populate with data and then run my unit tests. In memory databases are orders of magnitude quicker than a traditional database, you don't have to worry about setting up transactions to clean them up etc.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.