black sensei
black sensei

Reputation: 6678

Database not dropped in between unit test

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?

Upvotes: 0

Views: 886

Answers (2)

Esko
Esko

Reputation: 29377

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.

Upvotes: 1

Juha Syrjälä
Juha Syrjälä

Reputation: 34271

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.

Upvotes: 1

Related Questions