How to test the service layer

4

I'm developing a multi-layered and multi-module web design. For the persistence layer I am using JPA 2.1 and hibernate 4.2 and for the JUnit 4 tests. In this architecture my project was divided into classes:

  • GenericDAO (interface);
  • GenericDAOImpl (implementation);
  • EntityDAOImpl (inherits from GenericDAOImpl);
  • GenericService (interface);
  • GenericServiceImpl (implementation);
  • EntityServiceImpl (inherited from GenericServiceImpl);
  • Entity (POJO).

Of course these entities have dependencies between each other and one-to-one, one-to-many, many-to-one, and many-to-many mappings. I would like to test if these entities are being persisted, with their respective relationships, correctly in the database.

I started the development of these tests using an in-memory database (HSQLDB) provided by spring. I created a generic GenericServiceTest test class that tests the methods of my GenericServicesImpl classes and extended them to each specific entity, for example EntityServiceTest. An entity can be any object, user, account, city, country ...

Then when running the EntityServiceTest test class all JUnit methods and annotations located in the GenericServiceTest class are inherited and the test for this specific entity is performed.

To simulate the objects I used fixture objects (fake objects). But I am taking a considerable amount of work to test persistence between entities exactly on account of such relationships and the concurrent execution of the test methods. Problems of type: not-null attributes that must be retrieved from the database before saving the object that depends on it, and objects being saved in a method have already been saved in previously executed methods.

My questions are:

  • Is there a better way to test for persistence such relationships?
  • Is it common to test the persistence layer this way?
  • I can say that this type of test is integration, since I'm testing the integration between the service layer and the persistence layer?
asked by anonymous 19.01.2015 / 01:21

1 answer

1

You can avoid this work by using Arquillian .

It allows you to upload a portion of your program to the server and run the tests. Preferably in an environment identical to production.

In the way you are doing, the test can pass and the application fails when it is put into production, since the testing environment is different.

    
19.01.2015 / 02:05