Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
1  
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
2  
DBUnit is a JUnit extension that might help you. dbunit.org –  Mike Partridge Nov 16 '12 at 18:16
1  
I actually asked this today on SO - stackoverflow.com/questions/13413466/… . –  m3th0dman Nov 16 '12 at 19:49
add comment

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.

2 Answers

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
add comment

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?

etc

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
add comment

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