• 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

Mock doesnt work

 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
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
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?
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
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

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
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic