0

I want to test if created entity has been correctly persisted to database.There is a service integration test for create method:

@SpringApplicationContext({"setting ...."})
public class PersonServiceIntegrationTest extends UnitilsJUnit4 {
     @SpringBeanByName
     private PersonService personService;
     @Test
     public void createPerson() {
        String name = "Name";
        String sname = "Surename";
        DtoPerson item = personService.createPerson(name, sname, Arrays.asList( new DtoAddress("Pisek","CZE", true), new DtoAddress("Strakonice", "CZE", false) );
        Assert.notNull("Cannot be null", item);
        /* 
         *  This assertion fails because of transaction (I suppose) - item is not in
         *  database right now. 
         *  Why? Returned dto 'item; is not null?
         */
        //domain with all fetched related entities, eg. address
        Person p = personService.getPerson(item.getIdPerson());
        List<Address> addresses = p.getAddresses();
        Assert.notNull("Cannot be null", p);
        Assert.notNull("Cannot be null", addresses);//return collection of Address
        Assert.notFalse("Cannot be emtpty", addresses.isEmpty());
        ReflectionAssert.assertPropertyLeniens("City", Arrays.asList("Pisek", "Strakonice"), addresses);
     }
}
  1. Is it necessary to test create entity if I use hibernate? Someone can write you try to test low-level hibernate but hibernate has own tests. There is a trivial code above but I can imagine some specific code which persists more entites at same time (eg. one-many plus several one-one relations). And I want to test if relations has been correctly persisted.

  2. Is there a pattern to do test this way? I have a problem, that record is not at database. I don't want to use returned dto (it presents only agregate root entity - person, but it does not say about person basic data (one-many), person address (one-many) etc.)... i want to get persisted record.

tomascejka
  • 13
  • 6

2 Answers2

0

What I do to test the persistence is:

  • 1) I create the Domain entity,
  • 2) save it with Hibernate/JPA,
  • 3) flush and clear the hibernate session/entity manager
  • 4) load the entity again with hibernate
  • 5) compare the original entity with the one that I have (re)loaded

so I am pretty sure that the mapping is more or less correct and every thing get persisted

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • There is a problem. Dao tier don't allow service tier and service tier don't allow me to get entitymanager so there is no way to call entity manager api. I decide to rework service tier for person and divide responsibility. – tomascejka Feb 27 '13 at 05:43
  • this test is related to dao tier and domain objects. There is no service tier involved. - Anyway I think strict layering is relevant for the main code, but not so relevant for the testing code. – Ralph Feb 27 '13 at 06:19
  • "_this test is related to dao tier and domain objects_" - ok, now I understand your answer.... "_Anyway I think strict layering is relevant for the main code, but not so relevant for the testing code_" - it is interesting. If I have understood the second sentence correctly so test would have own hibernate session (and do not use service layer to loading data) to test eg. this test case. Right? – tomascejka Feb 27 '13 at 12:10
  • "So the test would have own hibernate session" - yes – Ralph Feb 27 '13 at 14:14
0

I decided to rework service method for create person.

  1. PersonService is responsible only to create domain entity Person - test will do only test the returned DtoPerson and its values.
  2. PersonService will inject AddressService, PersonBasicDataService, which they have own create methods with collection as parameter. These services will have own test classes and test only returned collection of DtoAddress or DtoPersonBasicData.

Tests will be simply and will solve only own responsibility. :-)

As @Ralph said in comments under his answer - this test case is not about service layer. There is necessary to test domain layer. And what there is a new idea which I do not use in integration tests - tests has own hibernate session.

Community
  • 1
  • 1
tomascejka
  • 13
  • 6