Print Page | Close Window

Inversion of Control pattern for unit testing

Printed From: One Stop Testing
Category: Types Of Software Testing @ OneStopTesting
Forum Name: Unit Testing @ OneStopTesting
Forum Discription: Discuss All that is need to be known about Unit Software Testing and its Tools.
URL: http://forum.onestoptesting.com/forum_posts.asp?TID=247
Printed Date: 28Jun2024 at 10:20pm


Topic: Inversion of Control pattern for unit testing
Posted By: Sameer
Subject: Inversion of Control pattern for unit testing
Date Posted: 23Feb2007 at 4:41pm
What is Inversion of Control?

Inversion of control pattern is used for loose coupling between components.

Components should program to an interface for services they require, this way the actual implementation can be changed at any time.

The creation of the object of the actual implementation should not be hard coded in the components.

The service consuming component doesn’t know anything about how to create the service providing component.

It is the responsibility of the service configurator or the container to inject the dependencies based on some configuration.

It is also called dependency injection because the container is creating the dependent objects and injecting them into the component. The dependency injection can be done using constructors or setters. You can either use your own service configurator or use a framework for IOC like spring, hivemind and picocontainer



How can it be used for Unit Testing?

Because the components are loosely coupled, a component can be tested without depending on other components.

When creating an interface the programmer can also create a mock implementation of the interface.

The component programmer can test his component by using the mock implementation during the testing, which can be later changed to the actual implementation.



Even if you know that there won't be more than one implementation of a service it is still better to create an interface and use IOC, because of its use in Unit Testing.



Print Page | Close Window