设计可测试性的构造函数 [英] Designing Constructors for Testability

查看:147
本文介绍了设计可测试性的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些现有的代码,试图添加它,并增加它的单元测试。



<$>

p $ p> public Info()throws Exception
{
_ServiceProperties = new ServiceProperties();
_SshProperties = new SshProperties();
}



我知道这是坏的,显然不可测试。在junit环境中,此类将无法每次都创建,因为它不能找到构建自身的必要属性。现在,我知道这个类将是一个更多的可测试与简单的改变移动任何以新作为参数。



所以我最终得到:



新建构函数:

  public Info(ServiceProperties srvProps,SshProperties sshProps)throws异常
{
_ServiceProperties = srvProps;
_SshProperties = sshProps
}

这允许我正确地单元测试这个Info类。问题是,现在所有的工作推到其他类:



一些其他类的方法:
$ b

  public void useInfo()throws Exception 
{
ServiceProperties srvProps = new ServiceProperties();
SshProperties sshProps = new SshProperties();
Info info = new Info(srvProprs,sshProprs);
doStuffWithInfo(info);
}

现在这个方法不可测试。所有我设法做的是推开这些Property对象的结构发生,其他地方的一些代码将被卡住,实际上必须调用新。



这里是对我的擦伤:我不知道如何打破这一连串的事件,简单地推这些新调用别的地方。

解决方案

查看使用依赖注入框架如春天。这种控制反转的应用意味着你的每一种类型都可以避免你所看到的陷阱,让配置连接组件在一起。



Introduction to Spring (pdf)给出了Spring的全面概述。



另请参阅 Martin Fowler的控制容器和依赖注入模式的反转


I'm working with some existing code, trying to add to it and increase the unit tests for it. But running into some problems with getting the code testable.

Original Constructor:

public Info() throws Exception
{
  _ServiceProperties = new ServiceProperties();
  _SshProperties = new SshProperties();
}

I'm aware that this is bad, and obviously not testable. In a junit environment, this class will fail to create every time since it wont be able to find the necessary properties to construct itself. Now, I'm aware this class would be a lot more testable with the simple change of moving anything prefaced with 'new' as a parameter.

So I end up with:

New Constructor:

public Info(ServiceProperties srvProps, SshProperties sshProps) throws Exception
{
  _ServiceProperties = srvProps;
  _SshProperties = sshProps;
}

Which allows me to properly unit test this Info class. The problem though, is now all that work is pushed to some other class:

Some Other Class' Method:

public void useInfo() throws Exception
{
  ServiceProperties srvProps = new ServiceProperties();
  SshProperties sshProps = new SshProperties();
  Info info = new Info(srvProprs, sshProprs);
  doStuffWithInfo(info);
}

Now this method isn't testable. All I've managed to do is push off where the constructions of these Property objects are occurring, and somewhere else some piece of code is going to be stuck actually having to call "new".

Here's the rub for me: I can't figure out how to break this chain of events of simply pushing these "new" calls somewhere else. What am I missing?

解决方案

Look at using a Dependency Injection framework such as Spring. This application of Inversion of Control means that each of your types can avoid the pitfall you've seen, leaving the configuration to "wire" components together.

This introduction to Spring (pdf) gives a comprehensive overview of Spring. The first chapter or two should be sufficient to explain the concepts.

Also see Inversion of Control Containers and the Dependency Injection pattern by Martin Fowler

这篇关于设计可测试性的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆