• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JUnit tests on the database CRUD functions.

 
Jiafan Zhou
Ranch Hand
Posts: 193
Fedora Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34870
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jiafan Zhou
Ranch Hand
Posts: 193
Fedora Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34870
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 193
Fedora Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34870
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic