aspose file tools*
The moose likes Testing and the fly likes JUnit tests on the database CRUD functions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit tests on the database CRUD functions." Watch "JUnit tests on the database CRUD functions." New topic
Author

JUnit tests on the database CRUD functions.

Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

Hi all,

I need to write some JUnit tests for the database CRUD functions. However, it is a client/server(DB) architecture where the database layer is located in the server side. How can I write JUnit tests for these CRUD functions on the client JVM?

Thanks


SCJP, SCJD, SCWCD, SCBCD, SCEA
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Same way as anything else--insert some data, read it back to make sure it's there (or has changed, or whatever else). That's more of an integration test, though, rather than a unit test, since an external system is involved.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

Junit tests are bit different from the manual unit test / integration test.

Your Junit test cases should run standalone without depending on server / database for proividing consistent results.
You just need to test your business logic in your class. So you can mock the outside world.

We have many mock frameworks for this purpose like jmock, easymock, mockrunner etc.,

In your case you need to mock the database with a mock framework. you can use DbUnit framework for this purpose. DbUnit is a mock framework to mock database.

DbUnit


SCJP 5, SCWCD 5
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

Prabhakar,
That would be for unit tests. JUnit is also used for integration tests which do involve the database/server.

Jiafan,
How does the real front end/client app connect to the server? Remote call? Your JUnit test would do the same thing.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

David Newton wrote:Same way as anything else--insert some data, read it back to make sure it's there (or has changed, or whatever else). That's more of an integration test, though, rather than a unit test, since an external system is involved.


Sorry, David. Maybe I was not clear enough about my initial question. I want to implement some white-box JUnit tests for the database CRUD functions. I would say integration test is a more high level black-box testing as opposed to low level JUnit tests. And it has to be the *JUnit tests* which are written in Java code. My concerns are since the database layer only exposed from the server side and are hidden from the client side, and the JUnit tests are written from the client side, in other words, I cannot directly create the JUnit tests for the database layer CRUD functions. THIS IS NOT WHAT I WANT.

Prabhakar Reddy Bokka wrote:Junit tests are bit different from the manual unit test / integration test.

Your Junit test cases should run standalone without depending on server / database for proividing consistent results.
You just need to test your business logic in your class. So you can mock the outside world.

We have many mock frameworks for this purpose like jmock, easymock, mockrunner etc.,

In your case you need to mock the database with a mock framework. you can use DbUnit framework for this purpose. DbUnit is a mock framework to mock database.

DbUnit


Actually couple of people mentioned the "MOCK" objects to me and I am very curious about how the mock objects are used in the JUnit environment. I will definitely have a look at the DbUnit framework. It seems a right approach. Thanks.


Jeanne Boyarsky wrote:Prabhakar,
That would be for unit tests. JUnit is also used for integration tests which do involve the database/server.

Jiafan,
How does the real front end/client app connect to the server? Remote call? Your JUnit test would do the same thing.



On a high level, the application I am currently focusing on has the following architecture:

client (java standalone app) ---> (via RMI) ---> gateway methods ---> database layer (CRUD functions)

I wrote some JUnit tests which are located in the client side JVM, however they do not directly test the dabase layer (CRUD functions). They test the gateway methods in lieu. Since the CRUD functions are hidden from the client side, the gateway methods adapt the CRUN functions into some other methods. I know, this is a horrible description, but I am sure that you get it.

I am now thinking of creating a new database on the client side (identical to the database on the server) and after that, I can write JUnit tests directly towards the CRUD *in the client JVM*. What do you think of this?

Regards.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yes, you *can* create JUnit tests, you'll just need to go through whatever mechanism you're using to expose the database. This makes them an integration test, because you're relying on external functionality.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

Jiafan Zhou wrote:I am now thinking of creating a new database on the client side (identical to the database on the server) and after that, I can write JUnit tests directly towards the CRUD *in the client JVM*. What do you think of this?

How does that test the database access code? It is on the server, no?
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

Jeanne Boyarsky wrote:
Jiafan Zhou wrote:I am now thinking of creating a new database on the client side (identical to the database on the server) and after that, I can write JUnit tests directly towards the CRUD *in the client JVM*. What do you think of this?

How does that test the database access code? It is on the server, no?


The database access code is intended to deploy on the server JVM, but I think it is also possible to run the same code on the client machine as long as I set up the database correctly. So that the database access code can be tested on the client JVM.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

Jiafan Zhou wrote:The database access code is intended to deploy on the server JVM, but I think it is also possible to run the same code on the client machine as long as I set up the database correctly. So that the database access code can be tested on the client JVM.

Sure. But then you are testing the code and not the deployed environment. Is that the intent?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JUnit tests on the database CRUD functions.
 
Similar Threads
Passed with 361 out of 400
Unit testting for Struts application -JUnit or Cactus?
System Tests
How important is testing our test cases ?
Non J2EE transactional management in Java