• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JUnit tests on the database CRUD functions.

 
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic