aspose file tools*
The moose likes Testing and the fly likes Testing with Database connections - help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Testing with Database connections - help needed" Watch "Testing with Database connections - help needed" New topic
Author

Testing with Database connections - help needed

Betsy Camel
Ranch Hand

Joined: Oct 02, 2003
Posts: 119
Hi all,

I have a application with struts framework which has a DAO class, a bussiness object class. i am using strutstestcase for testing the struts app. but when it gets connected to the DAO(which has lots of session objects getting connected to the DB), the tests donot run. It gives a nullpointerexception. i guess this should be a in-container testing. can anyone give me an idea of which would be the best tool for testing this type of application. Please reply soon.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30973
    
159

Betsy,
You have a few choices:
1) Unit tests the DAOs/business object class independently via pure JUnit.
2) Use a tool like StrutsTestCase or Cactus to run your Struts actions in container.
3) Use a tool like HTMLUnit to test your URLs without a browser.
4) Use a tool like Selenium to test your URLs through a browser.


[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
Betsy Camel
Ranch Hand

Joined: Oct 02, 2003
Posts: 119
Hi jeanne,

your first point - test the dao's via pure junit requires clarification as the dao class has a lot of session objects which require servletcontext. can this scenario be tested via pure junit? i have tried still i get the same error. can you please help me in understanding how to test this scenario.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30973
    
159

Betsy,
You could mock out the servlet context or refactor your code to reduce the dependencies on the context. In the long run, the later is useful as makes your code more testable and maintainable.
Betsy Camel
Ranch Hand

Joined: Oct 02, 2003
Posts: 119
hi
i am getting the following error while testing database connections.

junit.framework.AssertionFailedError: Exception in constructor: testhasOAVRole (java.lang.NullPointerException
at com.gm.bpd.common.transaction.Session.connect(Session.java:69)
at com.gm.bpd.common.transaction.Session.<init>(Session.java:35)
at com.gm.bpd.admin.dao.testACLCUDDAO.<init>(testACLCUDDAO.java:15)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at junit.framework.TestSuite.createTest(TestSuite.java:131)
at junit.framework.TestSuite.addTestMethod(TestSuite.java:114)
at junit.framework.TestSuite.<init>(TestSuite.java:75)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.getTest(RemoteTestRunner.java:399)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
)
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.TestSuite$1.runTest(TestSuite.java:263)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


please find the class below.





Please help me resolve ASAP

[edited to add code tags]
[ April 16, 2008: Message edited by: Jeanne Boyarsky ]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30973
    
159

I think the problem is in this code. Does it print an error message?

You really should throw an exception, not just print the exception out. This way, it won't fail later with a null pointer. (Also the nested try/catch blocks don't do anything. You only need one level.)

Betsy Camel
Ranch Hand

Joined: Oct 02, 2003
Posts: 119
YEs i am getting a connectException. what does this mean ?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
It means your code is trying to connect to a database but fails in doing that. Is there a database? Do you intend to connect to an actual database? What does the ConnectionException say?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Testing with Database connections - help needed