How Dependency Injection Increases Test Probability

I read a template in Factory and came across articles suggesting using a Factory template in conjunction with dependency injection to maximize reuse and testability. Although I could not find concrete examples of this Factory -DI hybrid, I will try to give some examples of my interpretation code. However, my question is how this approach improves testability.

My interpretation:

So we have a class Widget:

public class Widget {
    // blah
}

And we want to enable s WidgetFactoryto control the construct Widget:

public interface WidgetFactory {

    public abstract static Widget getWidget();
}

public class StandardWidgetFactory implements WidgetFactory {

    @Override
    public final static Widget getWidget() {
        // Creates normal Widgets
    }
}

public class TestWidgetFactory implements WidgetFactory {

    @Override
    public final static Widget getWidget() {
        // Creates test/mock Widgets for unit testing purposes
    }
}

Spring DI ( API, ), , Guice - IoC; , WidgetFactory , . Spring config beans :

<bean id="widget-factory" class="org.me.myproject.StandardWidgetFactory"/>
<bean id="test-widget-factory" class="org.me.myproject.TestWidgetFactory"/>

<bean id="injected-factory" ref="${valueWillBeStdOrTestDependingOnEnvProp}"/>

:

WidgetFactory wf = applicationContext.getBean("injected-factory");
Widget w = wf.getWidget();

, ( ), , .properties, , Spring DI StandardWidgetFactory <<29 > .

?!?. , Widget. , , .

:

, , , , , Widget. , - :

public class Fizz {
    public void doSomething() {

        WidgetFactory wf = applicationContext.getBean("injected-factory");
        Widget widget = wf.getWidget();

        int foo = widget.calculatePremable(this.rippleFactor);

        doSomethingElse(foo);
    }
}

, , "mock Widgets" unit test Fizz::doSomething().

, : , , ( ). , .

, : ( ), , , Factories ?!?

way-overengineering! ? , , Factory, ?! . .

+5
6

DI/IoC , , , , . , , , -, ( ) ..

DI/IoC. , factory, .

public class Fizz {

    @Inject    // Guice, new JEE, etc. or
    @Autowired // Spring, or
    private Widget widget;

    public void doSomething() {
        int foo = widget.calculatePremable(this.rippleFactor);
        doSomethingElse(foo);
    }

}
+6

Fizz - DI, DI, bean, . DI - ( , ). , DI Fizz.

,

  • . , .
  • , , DI.

DI, - . ( , DI, -.)

, Factory.

(), ? , .

: : Factory?, DI.

+2

, : Injection Dependency java, .

DI , / Factory ..

+2

factory, Injection Dependency Fizz. . applicationContext, WidgetFactory . .

public class Fizz {
    private WidgetFactory wf;
    public Fizz(WidgetFactory widgetFactory) {
        wf = widgetFactory;
    }

    public void doSomething() {

        Widget widget = wf.getWidget();

        int foo = widget.calculatePremable(this.rippleFactor);

        doSomethingElse(foo);
    }
}

, , Fizz. Mockito, ( , ).

@Test
public void test() {
    WidgetFactory wf = mock(WidgetFactory.class); // mock is a Mockito function that creates mocks for you automatically.
    Fizz objectUnderTest = new Fizz(wf);

    // Test Fizz
}

applicationContext. , . , applicationContext .

<bean id="widget-factory" class="org.me.myproject.StandardWidgetFactory"/>
<bean id="fizz" class="org.me.myproject.Fizz">
    <constructor-arg ref="widget-factory"/>
</bean>

factory , factory . , , DI . , . , factory. , DI, , , , , factory .

0

. EJB3-, Dependency, .

, . - , . factory, Java, Java, ( ). JDK, , , , , , .

, Gang of Four, - , , . factory, , factory . , , .

, Guice, GOF factory Java.

0

DI . - , .

interface Widget {
}

class UserOfWidget {
   Widget widget;
   public UserOfWidget(Widget widget) {
      this.widget = widget
   }
}

Using spring dependency injection
<bean id="widget" class="ConcreteWidget"/>
<bean class="UserOfWidget">
   <constructor-arg ref="widget"/>
</bean>

In a test case
UserOfWidget uow = new UserOfWidget(new StubWidget()); 

DI factory - , .

, , DI. , - , . JNDI (, ). JNDI factory, bean XML .

To summarize, you don’t need factories with DI to make the code more verifiable. Just DI is enough. Factories are only needed to simplify the creation of objects that are difficult to create.

0
source

All Articles