Making your classes and methods observable

Observability, at the class level, is about how easy it is to assert that the behavior of the functionality went as expected. My main advice is to ensure that your classes provide developers with simple and easy ways to assert their state. Does a class produce a list of objects you need to assert one by one? Create a getListOfSomething in that class, which the test can use to get the generated list of objects. Does a class make calls to other classes? Make sure these dependencies can be mocked and your test can assert their interaction. Does a class make internal changes in its attributes, but the class cannot or does not offer getters to each of them? Make the class offer a simple isValid method that returns whether the class is in a valid state.

It has to be easy for the test code to inspect the class behavior. Whenever it is difficult to observe whether the program behaves as expected, reflect on how observable the classes are. Do not be afraid to introduce simple getters or simple solutions to facilitate your testing. Behavior that is easy to observe will make the test code much easier! Let’s look at two pragmatic changes I make in my code so it is more observable.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *