• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

database connections?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to write JUnit testcases for the methods i need to test. Each method needs to access data from the database. Is there a way to make one connection and run multiple tests or do I have to make a connection for testcase?
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it is unit tests it is advisable to avoid making db connections, and to hard code expected data within unit test

If it is integration/system testing, making database connections is valid. But to answer your question, I dont think JUnit as such provides any specific connection reuse mechanism

You would resort to using Connection Pool, but since performance is not a consideration while testing, you can get a new connection everytime to simplify your test logic

Padma
 
joseph cooper
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is unit testing.

I was working on having an expected result which I have hard coded as 12840 and then compare that to the actual result. To get the actual result I am going to need a connection to the database? Here is an example of what I am working with.

package agfg.dx.eric.rf;
import junit.framework.TestCase;
public class TestSectionMVR extends TestCase
{
public void testGetOrderId() throws Exception
{
Section_MVR mvr = new Section_MVR();
int policyClientRequirementId = 109073;
int expected = 12840;
assertEquals(expected, mvr.getOrderId(policyClientRequirementId));
}
}
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joseph, you should probably check out the various mock object frameworks that let you fake the JDBC interfaces instead of connecting to an actual database server.

For starters, here's one relevant article.
 
reply
    Bookmark Topic Watch Topic
  • New Topic