Mocking sql connections..

1,686 views
Skip to first unread message

bharat

unread,
Mar 11, 2011, 6:43:25 AM3/11/11
to mockito
Hi,
can somebody help me in creating a mocking object for sql
connection...
below is the log file I am getting.
I am getting the userId from the database, connection is got in the
SqlConnection class, then to DAO layer, then to the BO layer, taken to
another BO layer.

My test class is ValidationBOTest.

getUserIdTest(com.nielsen.outbound.test.ValidationBOTest) Time
elapsed: 0.235 sec <<< ERROR!
java.lang.ExceptionInInitializerError
at
com.nielsen.outbound.dao.SqlConnection.getConnection(SqlConnection.java:
28)
at
com.nielsen.outbound.dao.LoginServiceDAO.getUserId(LoginServiceDAO.java:
113)
at
com.nielsen.outbound.business.LoginServiceBO.getUserId(LoginServiceBO.java:
194)
at
com.nielsen.outbound.business.ValidationBO.getUserId(ValidationBO.java:
680)
at
com.nielsen.outbound.test.ValidationBOTest.getUserIdTest(ValidationBOTest.java:
31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at
org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:
98)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:
79)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:
87)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at
org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:
88)
at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:
51)
at org.junit.internal.runners.JUnit4ClassRunner
$1.run(JUnit4ClassRunner.java:44)
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:
27)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
37)
at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:
42)
at
org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:
62)
at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:
140)
at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:
127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:
338)
at
org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:
997)
Caused by: java.lang.NullPointerException
at com.nielsen.outbound.common.Utils.loadFile(Utils.java:116)
at com.nielsen.outbound.common.Utils.<clinit>(Utils.java:77)
... 31 more

Geoffrey Wiseman

unread,
Mar 11, 2011, 10:15:42 AM3/11/11
to moc...@googlegroups.com, bharat
On Fri, Mar 11, 2011 at 6:43 AM, bharat <bhara...@gmail.com> wrote:
can somebody help me in creating a mocking object for sql
connection...

Normally, one wouldn't mock something as detailed as an SQL connection, more likely to mock one's own interface, such as a DAO.  It's less clear from your stack trace what you've mocked, whether or not you've mocked anything yet and how wide an interface it has, so let's leave that point aside for a moment.

Looking at your actual stack trace:
 
Caused by: java.lang.NullPointerException
       at com.nielsen.outbound.common.Utils.loadFile(Utils.java:116)
       at com.nielsen.outbound.common.Utils.<clinit>(Utils.java:77)

So, when the Utils class initializes (static initializer, static variable initialization, etc.), something calls Utils.loadFile() and on line 116 you get a NullPointerException. Not sure at this stage how mocking your SQL Connection class is going to change that -- but then, I don't know what Utils.loadFile() does, and why you're getting the NPE.

  - Geoffrey
--
Geoffrey Wiseman
http://www.geoffreywiseman.ca/

szczepiq

unread,
Mar 12, 2011, 12:33:26 PM3/12/11
to moc...@googlegroups.com
>Normally, one wouldn't mock something as detailed as an SQL connection

Exactly. In general, use mockito for mocking DAOs when you're testing services. However, if you want to test your DAO, test it with the db :)

Cheers!
Szczepan

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.

bharat

unread,
Mar 17, 2011, 8:24:21 AM3/17/11
to mockito
Thanks Geoffrey,

In the Utils class, I have a static method which loads a property
file. thats creating the problem. I believe we cant mock a class which
has a staic method, right??

On Mar 11, 8:15 pm, Geoffrey Wiseman <geoffrey.wise...@gmail.com>
wrote:
> On Fri, Mar 11, 2011 at 6:43 AM, bharat <bharat....@gmail.com> wrote:
> > can somebody help me in creating a mocking object for sql
> > connection...
>
> Normally, one wouldn't mock something as detailed as an SQL connection, more
> likely to mock one's own interface, such as a DAO.  It's less clear from
> >   - Geoffrey
> --
> Geoffrey Wisemanhttp://www.geoffreywiseman.ca/

bharat

unread,
Mar 17, 2011, 8:26:46 AM3/17/11
to mockito
Hi Szczepan,
"However, if you want to test your DAO, test it with the db :)"
can you explain it more clearly,..

Dominik Obermaier

unread,
Mar 17, 2011, 8:44:05 AM3/17/11
to moc...@googlegroups.com
Hi,

if you want to test a DAO, test it with a real Database and do not mock
the database behaviour. Notice, that this is not a Unit test anymore (it
is not tested in isolation!), this is an integration test.

If you are using a non-local Database (like mysql, postgres, oracle
etc), then you could consider using an in-memory database (like hsql or h2).

I hope this information does help you :)

Dominik

Brice Dutheil

unread,
Mar 17, 2011, 8:56:05 AM3/17/11
to moc...@googlegroups.com
DAOs cannot really be unit tested. They should however be tested during an integration test phase.
They are the glue between your business code and the DB. Then thay shall be tested accordingly.

There is two strategy for that, either you test DAOs again your target database (Oracle, Postgres, Mysql, etc.), or you can use an in memory database like the great H2.
However in this last scenario you should make sure that you can create the database on the fly, there might be other problems with vendor specific features too. You should have a word with your DBA on that matter, so you can plan things.



And about your static code, you definately should refactor your code to make it testable, especially in this property stuff case. You might want to wrap this static code in a object that will be a dependency of your tested object.

-- 
Bryce


Dominik Obermaier

unread,
Mar 17, 2011, 8:26:22 AM3/17/11
to moc...@googlegroups.com
For mocking static methods, you could use Powermock
(http://code.google.com/p/powermock/)

But consider refactoring the code, because using Powermock is a
indicator for strong code smell ;)

Dominik

Reply all
Reply to author
Forward
0 new messages