swarup7m
swarup7m

Reputation: 183

Help writing JUnit for JDBC

I created one class,in which i am inserting values into SQL as follows:

  public class ABC{
     some code here..........
   ...............
    public void insertUsers(String firstName,String lastName,String location){
      pre.setString(1,firstName);

I created test class for this class.

I want to write test case for this method insertUsers(),using assert statement. how to write assert statement for above method.

Upvotes: 1

Views: 1238

Answers (4)

Pushkar
Pushkar

Reputation: 7580

Well if you really want to test updating your database you can do that. Usually people follow one of the below two approaches -

  1. Use Spring AbstractTransactionalDataSourceSpringContextTests This allows you to add any values to the database and then spring will take care and revert the values that you have inserted.

  2. Use a seperate database Just for your JUnit tests. You really dont need anything heavy. You can use something like the HSQLDB which is really a lightweight java database. This will allow you to have separate test data from your production/QA database.

After the above is done(and you have run the insert statement) simply run select statement from your JUnit to get the data and then compare the previous data with the actual data.

Upvotes: 2

murrekatt
murrekatt

Reputation: 6129

When doing unit testing one should avoid accessing external resources such as databases, filesystems, network etc. This is to keep the tests in memory (fast), but also isolated from external failures. You only want to test a specific part of some functionality in e.g. a class, nothing else.

What this means for you is that the conn variable (I assume is the db connection) needs to be mocked out. You can do this easily with something like dependency injection, which means you pass in things into your class when constructing it. In this case you would pass in an interface which has the necessary functions conn uses.

Then in production you pass in the real db connection object while in test you pass in a mock which you control. Hence, you can then check that ABC calls and does what you expect it to do with conn. The same goes for pre you're using.

You can see it like this: I would like to test class ABC, and in order to do that I need to see how it uses pre and conn, so I replace those with my own test implementations I can check after doing something with ABC.

In order to specifically help you with what you're doing you need to show what pre is and tell us what you intend to test.

Upvotes: 2

CloudyMarble
CloudyMarble

Reputation: 37566

I wouldnt test the insertion of the data to the database, actually its not performant to access database during unittesting, this can be covered threw automated functional GUI testing tools of your application.

what you may want to test is the generation of the expected queries, this can realised if you seperate the geenration and the execution of the statements, you will be able to compare generated statements with expected ones without having to access you database from the unitest.

Upvotes: 0

Andreas Dolk
Andreas Dolk

Reputation: 114757

A couple of remarks.

I'd use the standard assert during development only. It will check a condition and throw a runtime exception, if the condition evaluates to false.

If you expect illegal arguments, than it's much better to add some "normal" code to the method to handle those values or throw an IllegalArgumenException and write log entry.

Do not close the connection in this method! Do it only when you open/create the connection in the very same method. In larger applications you won't be able find out who closed the connection after a while. If the caller of insertUsers opened the connection, the caller should close it itself!

(more help possible if you tell us what exactly you want to test - the method parameters or if the insert was a success)

Upvotes: 1

Related Questions