• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A mock for every dependency?

 
Antonio Diaz Sanchez
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

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.

Regards,

Antonio
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Can you give an example of something you feel is unnecessary? Discussing it in the abstract gives "it depends."

When it is useful: When your dependency has some complex logic
When it is not useful: When your dependency class is a JavaBean with just geters and setters
 
Antonio Diaz Sanchez
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

*Thank you* for the replies.

For example:

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?

Regards,
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(And Mockito, my current favorite :)
 
Antonio Diaz Sanchez
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you.

The syntax of Mockito is really clear! (compared to other options).

Regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic