设计可测试性的构造函数

编程入门 行业动态 更新时间:2024-10-26 13:25:07
本文介绍了设计可测试性的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

设计可测试性的构造函数

本文发布于:2023-10-31 06:42:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!