Do I need to do unit tests of system entities?

3

I'm working on a project and I join a team, we were given the task of doing a series of tests with a system, testes de integração continua , testes unitários ....
The point is that all systems have classes of entities and rules of business. So, it's very interesting to test business rule classes.
My entity classes have just the same old one:

atributos  
getters() and setters()   
construtor()  
toString()  
equals()   
hashcode()  
compareTo()

But I have a question regarding the entity classes:
Should I make testes unitários for my entity classes (beans or pojos) since they do not have any business rules?

    
asked by anonymous 21.08.2016 / 22:01

2 answers

2

A unit test of getters and setters is not done, which are the methods contained in POJOS. For any other method you must create the unit tests. It is good to keep in mind that the idea of a unit test is that its execution may fail if there is a change in the target class / method of the test. That way it does not make sense to test POJOs because their methods are just recovery and modification by default, often even generated automatically.

    
29.08.2016 / 23:41
0

As Giuliana Bezerra said, it does not make sense to test getters / setters. The same applies to for equals (), hashCode (), and so on. The methods that should have their behavior tested are those that involve business rules and whose change can impact the evolution of the system - to avoid regression. Testing getters / setters does not add anything to your test suite.

At the moment of deciding if a unit test should be written I suggest thinking about some points:

  • What is the complexity of the method / class in question?
  • What is the importance of the method / class in the module / system / application? (ie how many classes depend on that code - this is an indication that that code may turn out to be a crucial point of failure in the system)
  • What is the possibility of this code being maintained in the future? (maintenance means changing the code and for this to be safer, nothing better than having tests that ensure the operation.) The more tests, the less fear we have to change a code)
  • PS: I find it strange that your entities only have these types of methods. Remember that object orientation involves attributes and behaviors. I think, without knowing much about the structure of your project, that maybe your classes should be rethought. I suggest reading the Martin Fowler's article on the Anemic Model to learn more.

        
    01.10.2016 / 03:58