Guest User

Untitled

a guest
Jun 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. /**
  2. * Example that shows how to unit test around a legacy constructor
  3. * that initializes all kinds of dependencies without being
  4. * reachable (in a reasonable manner at least).
  5. *
  6. * @author Melvin Hazeleger
  7. */
  8. public class LegacyConstructor {
  9. private BigDependency bigDep;
  10. private AnotherAwfulThing awfulThing;
  11.  
  12. // before
  13. public LegacyConstructor() {
  14. this.bigDep = new BigDependency(new HugeDBConnection());
  15. this.awfulThing = new AnotherAwfulThing();
  16. }
  17.  
  18. // trick: create an overloaded constructor, so legacy
  19. // code can still use old constructor.
  20. // However you can create instances using this constructor.
  21. // Now you can pass mocks, instead of trying to create
  22. // these terrible dependencies.
  23. public LegacyConstructor(BigDependency d, AnotherAwfulThing a) {
  24. this.bigDep = d;
  25. this.awfulThing = a;
  26. }
  27.  
  28. // further class implementation ...
  29. }
Add Comment
Please, Sign In to add comment