Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JUnit tests for Spring + Hibernate-driven application

 
Olexandr Prokhorenko
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

I am developing a JUnit tests for Spring + Hibernate driven application. It is a web application, JUnit tests are run during the build process, use the Spring injection for definite beans and tests methods from them. However, I have got in troubles. I have two methods long addElement(Element) and Element getElement(long). When I am testing the addElement(Element) method everything goes smoothly and fine, it returns the id of added element, which is auto-incremented every time when I am starting the test. The problem is that this element is never appears in the database (MySQL) and therefore I can't test getElement(long) because all it can return is null. Everything works correctly if I am testing add- and get-methods withing one test method. If I am separating them into two different test methods, I am getting null. Would you help me out or this is a normal situation and the data processing couldn't be tested this way? Thank you!

P.S. Sorry if my post goes to wrong category, I just think that this is more related to Hibernate, then to anything other.
[ February 07, 2006: Message edited by: Olexandr Prokhorenko ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A unit test involving a backing database will rollback any changes rather than commit them, so that the test can be repeatable -- and in any case, you rarely want testing to alter your database! This is normal. In your unit test of getElement, you will have to run the code that inserts data in the database first.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic