aspose file tools*
The moose likes Testing and the fly likes Testing persistence or method? 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 "Testing persistence or method?" Watch "Testing persistence or method?" New topic
Author

Testing persistence or method?

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
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 ]

java amateur
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Oops. Sorry about that. I clicked "edit" instead of "reply"...
Please forgive me.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Well, just hit "edit" again and clean up that mess...


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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Feb 08, 2004
Posts: 1281
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

Joined: Feb 08, 2004
Posts: 1281
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Feb 08, 2004
Posts: 1281
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Feb 08, 2004
Posts: 1281
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
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Feb 08, 2004
Posts: 1281
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
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Feb 08, 2004
Posts: 1281
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

Joined: Feb 08, 2004
Posts: 1281
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

Joined: Feb 08, 2004
Posts: 1281
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!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Testing persistence or method?