This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes New article: Dependency Injection and Unit Testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "New article: Dependency Injection and Unit Testing" Watch "New article: Dependency Injection and Unit Testing" New topic
Author

New article: Dependency Injection and Unit Testing

Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42958
    
  73
The current issue of the JavaRanch Journal has an article by Christophe Verré about Dependency Injection and Unit Testing. You can check it out here, and if you have comments or question, feel free to post them in this thread.
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 434



You have already noticed that FarmServlet is tightly coupled to the FarmEJBRemote instance, which was retrieved via a call to "FarmEJBUtil.getHome().create()". It makes it very hard to test. When unit testing, we don't want to use any database. We don't want to access an EJB server either. That would make unit tests both difficult to execute and slow. So in order to unit test the FarmServlet class smoothly, we'd better make it loosely coupled. To remove the tight dependency between FarmServlet and FarmEJBRemote, we could use a setter based injection:

In the second one, it still depends on FarmEJBRemote, doesn't it?


To be or not to be. It's a question.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
It looks like "FarmServlet is tightly coupled to the FarmEJBRemote instance" should maybe be "FarmServlet is tightly coupled to the FarmEJBUtil class".

Does that make more sense?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: New article: Dependency Injection and Unit Testing
 
jQuery in Action, 3rd edition