I m actually studying Unit testing, reading some articles on the internet and trying to understand how it works exactly, but it's not very clear for me actually.
What should I test ?
I have seen that it's a good thing to create interfaces and make our testable classes implement this interface (permitting to implement it in our test classes) but what are exactly the classes that I should test ?
For example, admitting that I have a project structure like this :
- Presentation Layer (MVC)
- Business Layer (Service / Repositories)
- DB Layer (Mongo, Mysql etc..)
Should I only test services and repositories ? Should I create an interface to all my classes, even if I have only one implementation of it (two with the test) ?
What does my test really return ?
I have read an article, in which the author explaning the way to test a DB access using mocks :
- There is a class "S" which is a business service implementing an interface "I", that manage the data taken from the db to provide business objects
- There is a class "C" which is a controller and has a renderView method, which send to the view an array of the business objects given by the service "S".
1/ In the first test, he tests the value returned by "S" on getBusinessObject only using a collection of String. So he only tests if he has data in the collection (null, the content after adding an item etc...)
2/ In the second test, he mocks the "S" service to use it to test the renderView and only tests if he gets 2 results when the mock object method returns 2 objects.
My question is, how can we say that the test is a representation of the real state of our programm ?
I mean, it's possible that we have to make a difficult job in a service, with condition, iteration etc...
I dont really understand the impact of making unit tests if we mock a class that
return "a string";
comparing to a classe that transform drastically a String in the method.
Maybe I didn't understand the aim of unit testing in fact.