因为项目组在实行Scrum,想在下一阶段的开发中实行TDD,因为我就为几个已经功能完善的模块添加Unit Test Case。在此过程中,就发现了一些问题。
问题一
在一个模块的一个主要业务方法中,需要从数据库中读取数据。
AllocateStream(String key, ServetlContext context)
{
..........
ApplicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
StreamDao dao = ApplicationContext.getBean("StreamDao");
.........
}
在上面的代码中,通过context来,经由Spring来获取Dao对象,此处我认为不需要将Dao对象直接耦合在业务方法中,可以把Dao对象当成方法的参数传递进来或者通过Spring配置文件来关联此Dao对象。以便减少编写测试代码过程中Mock的难度。由于该模块已经在Demo系统,因此不便修改此处代码,所以为了完成该方法的Unit Test,所以我只有通过mock ServletContex和Spring的ApplicationContex来完成该方法的Test。但在Mock Spring的Context过程中,发现了一个更大的问题。
问题二
在mock Spring的WebApplicationContext过程中,由于需要读取某配置文件,而读取配置文件的代码如下:
InitXX()
{
......
URL url = this.getClass().getClassLoader().getResource("/conf/xx.xml");
}
通过ClassLoader的getResource方法来获取配置文件,是以classpath为起始路径来查找该资源。问题就出在从classpath来查找资源。当该方法应用在demo系统中时,作为一个web项目,WEB-INF目录会作为classpath,因此通过ClassLoader的getResource方法可以定位查找到该资源。
但是在单独运行Test Case时,在MockSpring的WebApplicationContext的过程中,需要读取该配置文件,但存放该配置文件的路径并未添加到Classpath中,所以根本查找不到该配置文件。
为了解决这个问题,通过查看API,我给出了下面的解决方法:
用Class.getResource()方法来替代ClassLoader的getResource方法。调用Class.getResource()时,如果路径是不以"/"开头,则是从当前目录开始查找,如果路径是以"/"开头,则是从classpath开始查找。因此可以用下面的代码来替代上面的代码:
InitXX()
{
......
URL url = this.getClass().getResource("conf/xx.xml");
}
记录以上两个发现的问题,作为实行TDD的开端,以便以后查看。。。。
分享到:
相关推荐
Python Unit Test Automation will allow you to quickly ramp up your understanding of unit test libraries for Python 3 through the practical use of code examples and exercises. All of which makes this ...
pragmatic unit test with c#
简明扼要介绍了如何使用Sql developer去组织Oracle plsql的单元测试
VS2013不能自动添加默认测试类了,不知道微软处于什么原因去除了这个功能,但是对于像我这样用惯了自动生成的人都变懒了
Python Unit Test Automation will allow you to quickly ramp up your understanding of unit test libraries for Python 3 through the practical use of code examples and exercises. All of which makes this ...
Python Unit Test Automation Practical Techniques for Python Developers and Testers 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开
Python Unit Test Automation 英文无水印原版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息...
a demostration for unit test in c#
MR Unit test所需的jar包(不包含Hadoop环境jar包),适合Hadoop2,具体实例参考《hadoop编程小技巧(8)---Unit Testing (单元测试)》
Furthermore, an increasing number of professionals is leaning on a test-first style of programming, using automated tests not for protecting against regression but for aiding them in design, ...
dao-unit-test.ftl
test unit 2test unit 2
The art of unit test
NULL 博文链接:https://zongyukai20070419095606.iteye.com/blog/600730
一个Symbian OS Unit Test Framework
对于不可控的第三方接口、第三方服务以及不确定性的代码如何进行单元测试用例的编写提供思路。同时对如何编写可测试的程序提供一种思路。同时对如何编写可测试的程序提供一种思路。同时对如何编写可测试的程序提供一...
android-unit-test, Gradle插件向android插件添加单元测试 为Robolectric准备 单元测试 为android插件添加单元测试的Gradle插件。 为Robolectric准备。不推荐使用Android插件版本 1.1年以来,在本机支持的单元测试。...
NULL 博文链接:https://15609845237.iteye.com/blog/2424730
Jsmaine for unit test JavaScript, 用来做JavaScript单元测试的利器,详细情况请关注博客:http://blog.csdn.net/ghostbear
一款基于 Visual C++ 针对 C 语言程序,帮助提供单元测试环境的小工具:打桩、用例管理(目前暂不支持)、代码静态扫描,以及函数复杂度分析等等。本工具仅仅限于技术交流,不可以用于商业目的。