English 中文(简体)
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("some@email.com");
  ....

  ....
  ob2.setEmail("someother@email.com");
  ....
  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?

最佳回答

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.

问题回答

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签