views:

112

answers:

2

Hello good people i came accross a weird behaviour in my test.I'm using JPA hibernate annotation with spring. let say i have an Class MyObject and it's property email is marqued

@Column(name="EMAIL", length=100, unique=true)
private String email;

i prepare for what i need to be in the database in the setup of this class MyObjectDAOImplTest

@Autowired
MyObject1 ob1;
@Autowired
MyObject1 ob2;

@Before
public void setUP(){
  dao = manager.createthedao();

  ....
  ob1.setEmail("[email protected]");
  ....

  ....
  ob2.setEmail("[email protected]");
  ....
  dao.save(ob1);
  dao.save(ob2);

}

so my a part from the fist test method all the reste are failling.I's about duplicates values on the email column but my hbm2ddl.auto=create and i even used the create-drop. but still. i just don't get it. i've used this in so many project without the unique of course but i expect the database to be dropped each time a test method is run.Is there anything about the unique i should be aware of ? thanks for reading.Give me your suggestion.Did i left out something or fail to do some?

+1  A: 

Shouldn't you have some code to drop/remove the unit-test database after (or preferably before) each test? Are you sure that you are actually creating the database at all? What database engine you are using?

If you are using some memory based database, are you initializing it in the right place (every time a test is executed)?

Are you calling SessionFactory.close() somewhere? If you are using hibernate.hbm2ddl.auto=create-drop, that should handle the dropping of the database.

Juha Syrjälä
thanks for the reply here is my hsql.jdbc.propertiesjdbc.driverClassName=org.hsqldb.jdbcDriverjdbc.username=sajdbc.password=jdbc.url=jdbc:hsqldb:file:mydb;create=truei've switched to mysql because of this problem.when testing with hbm2ddl.auto=create-drop and then i've commented all my test method but the first one.and saw the db dropped after i refreshed the db browser.
black sensei
Please, add all relevant config files to your question.
Juha Syrjälä
btw. maybe you should consider using in-memory mode for testing. (jdbc.url=jdbc:hsqldb:mem:mydb;create=true)... it should be faster.
Radek Vařbuchta
+1  A: 

You're missing @After method which is why you're seeing this behaviour. When running jUnit 4.x tests, the whole suite is run in a single thread one after another which means that you have to clear the state yourself or unspecified behaviour occurs, usually resources keep hanging and cause side effects to other unit tests.

Esko
mmmh didn't know that it's the case.i've finaly done that.but in my mind it was i work around because i thought i'm not supposed to do it
black sensei