Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Mocking injected Dependencies  RSS feed

 
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
Sheriff
Posts: 37249
519
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!
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!