aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Mocking injected Dependencies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Mocking injected Dependencies" Watch "Mocking injected Dependencies" New topic
Author

Mocking injected Dependencies

Thorsten Mueller
Greenhorn

Joined: Dec 19, 2010
Posts: 5
Hi everybody,

Assume I want to test a class like this:


I want to test outside the container and therefore need to mock the dependencyA. What is the common practice to do this? Do you write an additional constructor that passes dependecyA to get access to or are there better ways?

Best Regards

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

Thorsten,
The most common approaches are:
  • write a setter
  • write a constructor
  • set the field directly (since it is package private in your example)
  • set the field using reflection


  • [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
    Thorsten Mueller
    Greenhorn

    Joined: Dec 19, 2010
    Posts: 5
    allright, I think then I take the constructor!

    Thank you!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Mocking injected Dependencies