I’m trying to test my code, written using JBoss Seam and Hibernate. What I’d like to achieve is this:
Execute a SeamTest automated test to run a particular method on a Seam component. This component performs a database update (merge or save). I’d like to cause this update to fail, allowing me to test my error-handling code and ensuring my code recovers gracefully.
I’ve not been able to determine a simple way of doing this.
Any suggestions would be appreciated.
Should the update fail because of a (simulated) malfunction in Hibernate or because your entity-validation did not succeed? I claim you hardly run into the former case. I never experienced Hibernate problems that weren’t caused by my code. For such situations, you have to make sure that your transaction is rolled backed if a Hibernate exception occurs. Normally, if you have annotated your action method in Seam with
@Transactionalyour transaction should be rolled back upon aRuntimeException(eachHibernateExceptionis aRuntimeException).For the latter case (testing your entity-validation) you can create a
ComponentTestthat sends an invalid entity to your update method.Another situation you could check is what happens if you call your method with a
nullentity to be updated or with an entity that doesn’t exist on the DB.If you post the code of your update method, I’ll try to give some more suggestions.