This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mocking injected Dependencies

 
Thorsten Mueller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  •  
    Thorsten Mueller
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    allright, I think then I take the constructor!

    Thank you!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic