• 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

Testing persistence or method?

 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is, what do you want to test? Knowing that, it would be easier to tell whether you're testing that something or whether you could test that something in some other, perhaps easier way.

I suspect you're trying to test this method:
ConvencaoDao.criaOuActualiza(Convencao)

Could you post that method for us to see? If I'm guessing correctly, it might make sense not to test this method's implementation as part of your unit test suite and rely on your functional tests.
[ March 09, 2005: Message edited by: Lasse Koskela ]
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops. Sorry about that. I clicked "edit" instead of "reply"...
Please forgive me.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, just hit "edit" again and clean up that mess...
 
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
hope this is my original post:
I've an object that�s gonna belong to two Sets (
Set contratos=new HashSet() :
class Entidade has an add(Contrato c) method and so does class Convencao.
When i create an object from class ContratoConvencao i add it to both Entidade and Convencao Sets.
I'm using hibernate and i'm testing.
here's some of my code:

when i check collection's size, am i testing what really comes from database or i'm just testing the method (and in this case wasnt necessary persistence)?
hope i made myself clear enough
thanks in advance
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks the same

So, as I was saying...

The question is, what do you want to test? Knowing that, it would be easier to tell whether you're testing that something or whether you could test that something in some other, perhaps easier way.

I suspect you're trying to test this method:
ConvencaoDao.criaOuActualiza(Convencao)

Could you post that method for us to see? If I'm guessing correctly, it might make sense not to test this method's implementation as part of your unit test suite and rely on your functional tests.
 
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 want to assure myself that when i create (and persist) a ContratoConvencao and add it to Entidade and Convencao collections, that works.
Before this i run some tests (you called it functional tests) and those methods worked well.
Now, that i'm testing persistence, i want to be certain that besides ContratoConvencao being persisted it also belongs to the other 2 collections.
And this because i want to later test how cascades behave: if i remove it from one collection does it get removed from the other too and so on.

My code:
 
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
please let me refactor my Q
imagine that:
i've class Boss that has a contracts Collection
i've a class Shop that also has a contracts Collection
i've a class Contract
when boss and shop celebrate a deal, a Contrat is created and stored by both
What's the best aproach for testing that contract is persisted (on its own) and added to both colections?
using database tests?
using functional tests?
other?

note: after rereading this i get the impression i'm trying to test to diferent things at a time
anyway i'd love your opinion and help
thanks in advance
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I was thinking as well. You could test the persistence separately and have separate tests that exercise the collaboration between your objects, i.e. associating Bosses with Contracts and so forth.
 
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 your answer

i've done it: tested persistence (before) and afterwards i tested coupling

one curious thing is that hibernate wont let me delete Contrato unless i first remove it from both collections
 
Lasse Koskela
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:
one curious thing is that hibernate wont let me delete Contrato unless i first remove it from both collections


Ah. I've seen something like that as well. Probably something to do with cascade configurations of your domain objects.
 
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
with cascade="none" all goes fine but with "save-update" i get an ugly ObjectDeleteException:
deleted object would be re-saved by cascade (remove deleted object from associations)
 
Ilja Preuss
author
Posts: 14112
  • 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:
with cascade="none" all goes fine but with "save-update" i get an ugly ObjectDeleteException:
deleted object would be re-saved by cascade (remove deleted object from associations)



Doesn't that actually make sense?

If I understand you correctly, you are removing the object from one of the collections. That would mean (with cascade activated) that the object also should be deleted from the database, *but* it is still needed by the other collection, so it also needs to *remain* in the database. Kind of a conflict, isn't it?
 
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 I understand you correctly, you are removing the object from one of the collections. That would mean (with cascade activated) that the object also should be deleted from the database, *but* it is still needed by the other collection, so it also needs to *remain* in the database. Kind of a conflict, isn't it?


what i was refering to was that i was trying to delete a Contrato (from db)- to which hibernate opposes - without first removing it from collections
 
Ilja Preuss
author
Posts: 14112
  • 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:

what i was refering to was that i was trying to delete a Contrato (from db)- to which hibernate opposes - without first removing it from collections



Yes, OK. If you delete the "Contrato" from the db, and save one of the collections, what would hibernate be supposed to do?
 
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 delete the "Contrato" from the db, and save one of the collections, what would hibernate be supposed to do?



you just showed my tests are incomplete!
i've allready stated elsewhere that i'm an amateur and so i can only work in java in my spare time
at this point i've planed 3 things: complete this article (where btw im stuck again ), and then, with my app, re do my tests, because i guess i'm using too much database tests so i want to use mocks (lets see if i'll succeed) and then introduce your idea.
About your idea: i guess these must be the tests i have to use db connection, hibernate, mappings and all that stuff to really test if its working the way i intent
as to the others (those that involve persistence as well) i''l try to use mocks
as soon as i've some results i'll let you know
 
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
after all i started with getting an answer to you
here are my tests and stack trace


net.sf.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): 2, of class: logica.ContratoConvencao
at net.sf.hibernate.impl.SessionImpl.forceFlush(SessionImpl.java:761)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:739)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1385)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
at net.sf.hibernate.impl.SessionImpl.preFlushEntities(SessionImpl.java:2693)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2270)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2259)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at persistencia.ContratoConvencaoDao.elimina(ContratoConvencaoDao.java:92)
at persistencia.TesteCascades.testCascade_all_delete_orphan(TesteCascades.java:129)
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)



i tested with both extreme scenarios: first with cascade="none" and second with cascade="all-delete-orphan"

from the fisrt i realized Collections dont interfere with each other
with second i confirmed what i said here i earlier post

as to your Q i just cant answer because either i've to remove Contrato from both Collections (in which case i guess nothing interesting happens) or if i dont do it, then hibernate wont let me delete contrato from database...

i'd like help from someone reading this:
1) can i profite of teardown method to make something usefull?
2) this test takes 3,755 sec plus time to create db (another 3+ seconds)
from what i've read, it would be enough to have a test to test if persistence is indeed ocurring; if true, i'd make all other persistence tests with mocks
My Q: which database specific tests would you recomend?

thanks in advance

EDITED
i think i'v an answer for you
i added this (green) test:

[ March 12, 2005: Message edited by: miguel lisboa ]
 
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

this test takes 3,755 sec plus time to create db (another 3+ seconds)


i'm amazed!
i was using mysql to achieve that performance, now i'm using hsqldb and it takes .... 0,261 sec plus less then half a sec to build db!!
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic