This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am new in the testing world. Although I think I have understood the TDD process, it would be great if someone could give me same light about a basic issue.
Specifically, I don't know whether the process suggests to create Mocks even if the dependency arises from other class defined by the same application. I understand that it is valuable creating mocks for layers as the network and the database ones, but is it really considered a good practice creating a mock for every dependency of the class? We have to work with legacy code and we find that applying this way of working is a bit difficult because most of the classes haven't been coded in accordance with the injection pattern.
Depends on what you're testing: if you're testing multiple components at the same time you're not really unit testing, you're integration testing.
Note that static classes can be mocked--but IMO it's better to change the system over time to avoid their use and ease testing. Sometimes it's enough to create an instantiable wrapper that just calls the static methods.
The classes A and B have been developed by me and belong to the business logic of the application. The class A has a member of the type B, which is used by A to call methods of the class B.
if I have understood correctly, I should:
a) Develop a mock for the class B.
b) Make that the class B and its mock implement a common interface.
c) Use injection to pass to the class A an objetct that implements that common interface (through the constructor)
d) Pass to A the mock object of B during the tests.
Is it right?
author & internet detective
Yes. Two notes:
1) If B does something trivial, it could be worth skipping the mock. In reality, it is more likely it does something worth mocking out.
2) You don't have to write a mock that implements the interface. easyMock and jMock dynamically create it for you.