Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is unit-testing of accessors a must?

Tags:

unit-testing

For classes that have several setters and getters besides other methods, is it reasonable to save time on writing unit tests for the accessors, taking into account that they will be called while testing the rest of the interface anyway?

like image 727
Yarik Avatar asked Feb 27 '09 16:02

Yarik


People also ask

Is unit testing always necessary?

Unit tests are also especially useful when it comes to refactoring or re-writing a piece a code. If you have good unit tests coverage, you can refactor with confidence. Without unit tests, it is often hard to ensure the you didn't break anything.

Do you need unit tests for getters and setters?

Properties (getters/setters in Java) are good examples of code that usually doesn't contain any logic, and doesn't require testing.

What must be tested by a unit test?

Therefore, automated unit tests should make up the bulk of your tests. Unit tests should validate all of the details, the corner cases and boundary conditions, etc. Component, integration, UI, and functional tests should be used more sparingly, to validate the behavior of the APIs or application as a whole.

When should the unit testing be performed?

Unit testing is the first testing phase and it is practiced before moving to the phase of integration testing. Hence, before moving for the next testing level, make sure to fix all the identified bugs in the unit testing phase.


2 Answers

I would only unit test them if they do more than set or return a variable. At some point, you need to trust that the compiler is going to generate the right program for you.

like image 53
dhable Avatar answered Oct 19 '22 04:10

dhable


Absolutely. The idea of unit tests is to ensure that changes do not affect behavior in unknown ways. You might save some time by not writing a test for getFoo(). If you change the type of Foo to be something a little more complex then you could easily forget to test the accessor. If you are questioning whether you should write a test or not, you are better off writing it.

IMHO, if you are thinking about skipping adding tests for a method, you might want to ask yourself if the method is necessary or not. In interest of full disclosure, I am one of those people that only adds a setter or getter when it is proven necessary. You would be surprised how often you really don't need access to a specific member after construction or when you only want to give access to the result of some calculation that is really dependent on the member. But I digress.

A good mantra is to always add tests. If you don't think that you need one because the method is trivial, consider removing the method instead. I realize that the common advice is that it is okay to skip tests for "trivial" methods but you have to ask yourself if the method is even necessary. If you skip the test, you are saying that the method will always be trivial. Remember that unit tests also function as documentation of the intent and the contract offered. Hence tests of a trivial method state that the method is indeed meant to be trivial.

like image 29
D.Shawley Avatar answered Oct 19 '22 02:10

D.Shawley