Where use unit test?

I wanted to make a post about Mockito, but I think the answer for title questions is the necessary knowledge before it. So what do you think about it? All code should be tested or only core? I have my option about it and I share it for you.

I don't test all code using unit tests

I begin from the easier sides. I don't test getters and setter, because it is stupid. If you don't use setters or getters on your methods, maybe you don't need it. Sometimes of course you use the library which map an object using reflection, so You must have these data only for storage. I know, but in other cases you test your getters and setters during testing functionality which using it.

A unit tests are bad for facade implementation, because I think this is a good place for integration tests. Why? Integration tests are for check how components working together. If you want to check how methods work together I feel you want to do the same.

The next place is integration with other systems and application. That place is for integration test too. If you have the possibility to use the other system for testing I think You should use integration tests. In these places you should only test create data for it or feedback services, but single responsibility tells us that it will be other functions.

The last fragment is configuration, because we can't test it other than copy paste it.

Only these fragments of codes I skip. Do you think there was a lot of code for testing. I think NO! A lot of our code only uses blocks which should be tested using unit tests. Maybe in other projects than a web site application is more testable code, but in this way I think a lot of code is only facade and we do not even realize it.

What you should test like unit

An algorithm for data processing on globally meaning. If you filter data, sort, find and etc. You should test it like unit. This fragment of code is a very sensitive to bug. Another case is data preparation and service. That is less sensitive to bug, but It is your business logic, so it is important.

A business exceptions should be tested, because you should be sure that it work fine and you do it on exception case. 

Conditions are good fragments for testing, because being problematic and sensitive to bug.

Validators, operations on objects, data generators and utility functions.

What you can test like unit for a greater good

You can test a library for understanding and be sure what it works. This is good practice if you have time or if you want check it. If you have that tests you can calmly upgrade it when your tests working fine. A lot of programmers and company don't have time for it, but if I can do it, I will do it. 

For the end.

If you have any question about a specific fragment of code or about how testing feel free put the question in the comment.

Comments

Popular posts from this blog

Why TDD is bad practice?

How correctly imitate dependencies?

Software development using Angular 5 with Spring Boot