• 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: 34965
378
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