Posted by: terryhowe | June 29, 2017

Dependency Injection for Testable Code

One of the easiest ways to make code testable is to use dependency injection.  For example if you need to create an object with some application level configuration you could either have your configuration class as a singleton or pass the configuration into to the class constructor.  If you pass the configuration into the class with dependency injection, it is much easier to test.  Your test code can create various mock configuration classes to test various scenarios.  Sometimes I feel making code testable causes me to make different design decisions, but I think having easily testable code is more valuable.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: