aspose file tools*
The moose likes Testing and the fly likes A mock for every dependency? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "A mock for every dependency?" Watch "A mock for every dependency?" New topic
Author

A mock for every dependency?

Antonio Diaz Sanchez
Greenhorn

Joined: Mar 26, 2010
Posts: 6

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: May 26, 2003
Posts: 30965
    
158

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


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Antonio Diaz Sanchez
Greenhorn

Joined: Mar 26, 2010
Posts: 6

*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

Joined: May 26, 2003
Posts: 30965
    
158

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

Joined: Sep 29, 2008
Posts: 12617

(And Mockito, my current favorite :)
Antonio Diaz Sanchez
Greenhorn

Joined: Mar 26, 2010
Posts: 6

Thank you.

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

Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A mock for every dependency?