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

how dependency injection easy unit testing

 
sam liya
Ranch Hand
Posts: 1240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how dependency injection reduce unit testing effort?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DI allows the substitution of implementations. For example, you could create a service that always throws a database exception wrapper to simulate error conditions in your mainline code. This allows you to test your application's error handling/flow.
 
stanislav bashkirtsev
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reduce unit testing effort?
Who said that? o_O
For example, you could create a service that always throws a database exception wrapper to simulate error conditions in your mainline code. This allows you to test your application's error handling/flow.
Mock-frameworks are about this issue.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, mock frameworks *create* the object that will be injected.

Without the ability to inject an object mocking is not terribly helpful.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes testing easier, we could inject mock objects via XML without changing any source code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic