aspose file tools*
The moose likes Testing and the fly likes Mock doesnt  work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Mock doesnt  work" Watch "Mock doesnt  work" New topic
Author

Mock doesnt work

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
its my first one;
i've a class ConvencaoDao (which i want to mock) that implements InterfaceDao. I'm trying to test adicionaOuActualiza() method.

Here's what i've got so far:


and i get this:
net.sf.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)
at net.sf.hibernate.impl.SessionImpl.checkId(SessionImpl.java:2661)
at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2485)
at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2478)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2280)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2259)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at persistencia.ConvencaoDao.criaOuActualiza(ConvencaoDao.java:56)
at persistencia.TesteMock.testCria(TesteMock.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
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:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)


What i understand is that hibernate is being called, which wasnt suposed to happen; otherwise i dont know how to sort this out.

Can someone please give me a hand?

thanks in advance

edited

i realized if i changed the criaOuActualiza(obj) to simply:

the test gets green.

My understanding of mocks is that we use them when one doesnt want to create a real obj - just uses a fake one.

so right now i'm quite puzzled: does mocks imply using tdd?

i mean, if i hadnt the really code, my test would pass; this way, as i have allready implemented the method, the test gives an error.

so how can i test my Daos without actually connecting to db?


[ March 14, 2005: Message edited by: miguel lisboa ]

java amateur
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

Miguel,
The following line of code is the one that is causing the problem:

While you are mocking out the Connection, you are not mocking out the hibernate session. So Hibernate tries to get a session and fails because it is a unit test and the db is not available.

Mocks do not imply TDD. Mocks only imply unit tests.

Out of curiousity, why are you passing an object to the method, instead of a Connection directly?


[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
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
While you are mocking out the Connection, you are not mocking out the hibernate session. So Hibernate tries to get a session and fails because it is a unit test and the db is not available.

i'm not sure if i agree with you - my dao deals with hibernate persistence, so my methods save, update, delete and list all my objects, in this particular case Convencao objs.
Mocks do not imply TDD. Mocks only imply unit tests.

ok
Out of curiousity, why are you passing an object to the method, instead of a Connection directly?

using hibernate, one saves a pojo saying:

so i'm saving my Convencao obj
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
If you want to test your DAO, you don't need to mock itself, but the objects it needs to collaborate with. In your example that would be the hibernate session (wich is a little bit hard to mock, unfortunately).

Does that help?

As an aside, you might want to look at EasyMock or JMock, which are more advanced dynamic mocking libraries.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
If you want to test your DAO, you don't need to mock itself, but the objects it needs to collaborate with. In your example that would be the hibernate session (wich is a little bit hard to mock, unfortunately).

1) i conceed: Jeanne was right
2) i'll wait for my just won agile java (ch 12, i guess)
Does that help?

you made a point - i was quite unsure about all this mock stuff and then, i see i didnt get it quite rightly (to say the least)

As an aside, you might want to look at EasyMock or JMock, which are more advanced dynamic mocking libraries.

dont know which would be better suited for me - at least both have tutorials

thanks for your post!
[ March 15, 2005: Message edited by: miguel lisboa ]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

Originally posted by miguel lisboa:
so i'm saving my Convencao obj

Actually you are saving a casted convencao object.

If saveOrUpdate takes an object, there's no need to cast at all.

On easyMock vs jMock, I use easyMock. Mainly because it was first to come out of the two. I don't find jMock to offer enough benefits to consider switching. That said, I would use jMock if I had a clean slate.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i followed jMock's tutorial and when i wanted to run it ... dont know how...
tried adding a main but didnt know where to go from there
can someone tell how to do it? (i browsed docs and googled too, but didnt help)

thanks in advance
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by miguel lisboa:
i followed jMock's tutorial and when i wanted to run it ... dont know how...
tried adding a main but didnt know where to go from there
can someone tell how to do it? (i browsed docs and googled too, but didnt help)

You run your jMock tests just like your regular JUnit tests. Could you post one of your test*() methods that uses jMock?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks for helping

i simply tried to reproduce the aforementioned article, both from command line and from within eclipse. As class Publisher had no implementation, i did it like this:

as to my test class:

if i dont have a main, compiler complains; if i ty running it as a JUnit test, eclipse says no JUnit test found...
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
uff!!!

finally this code runs ok from both eclipse and command line:


had to change from once to never in order to get a green; i'm unsure about the reason(s) why...

its a pitty many tutorials have (minor) bugs ; in this case the class should be... public

BTW
@Lasse
(remember this)
in case you'r interested you could advertise that test infected tutorial's code complete can be found in JUnit download
[ March 20, 2005: Message edited by: miguel lisboa ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock doesnt work