I've read and found numerous articles that suggest on how to unit (rather, integration) test your DAOs backed by an ORM framework.
Here's one example on how to test Hibernate - http://ift.tt/1Zp2Uw3
That seems pretty clean and easy to understand. But I fail to understand -
Why do we need another component that prepares data in the database for the test(DbUnit in the example that I've mentioned), when we could use the ORM itself?
To quote the e.g. used in the link that I've mentioned above -
@Test
@DatabaseSetup("sampleData.xml")
public void testFind() throws Exception {
List<Person> personList = personService.find("hil");
assertEquals(1, personList.size());
assertEquals("Phillip", personList.get(0).getFirstName());
}
Why could it not be just done this way -
@Test
public void testFind() throws Exception {
Person person = new Person(1, "myName") //Person(int id, String firstName)
personService.save(person);
List<Person> personList = personService.find(1);
assertEquals(1, personList.size());
assertEquals("myName", personList.get(0).getFirstName());
}
What is wrong with this? I don't remember which one, but one article mentioned that doing it this our testFind method would be depending on the personService#save method which is against the practice of "tests have to be independent of each other". Is this the right and if right the only reasoning behind it?
I also understand that DbUnit is used because it would bring back your test database to a "known state between test runs". We could still achieve it by rolling back the transaction after we complete the test. (Which would mean it the database would have no data between tests.)
Aucun commentaire:
Enregistrer un commentaire