使用Junit时源代码和测试代码的组织

http://www.west263.com/info/html/chengxusheji/Javajishu/20080226/47898.html

近来在学习使用 Junit test framwork .关于源代码和测试代码的组织问题我看了一些资料。其中有这样的建议: 
1. Create test cases in the same package as the code under test. For example, the com.mydotcom.ecommerce package would contain all the application-level classes as well as the test cases for those components.

1。在同一个包下测试的代码创建测试用例。例如, com.mydotcom.ecommerce的包将包含所有应用程序级别的类以及这些组件的测试用例。

2.To avoid combining application and testing code in your source directories, create a mirrored directory structure aligned with the package structure that contains the test code.

2.To避免相结合的应用和测试代码的源代码目录,创建一个镜像的目录结构与包含测试代码的包结构。

(http://www.clarkware.com/articles/JUnitPrimer.html)

就是说测试代码应该被测试代码放在同一个package中,但是测试代码放在一个独立的目录中.也就是组织成下面这样:

1.     esrc
2.       com
3.         s xyz
4.              HelloWorld.java
5.      test
6.        com
7.     

http://www.west263.com xyz

HelloWorldTest.java

这样是比较合理。这样既可以方便地测试protected方法/域,同时不会导致源代码和测试代码混在一起,难于管理。

如果使用的是Eclipse,只需简单设置一下就可以了。 project -> properties -> Source标签 addFolder 按钮。

或者直接在classpath文件中直接设置也可以。将

<classpathentry kind="src" path=" "/> 换成:

<classpathentry kind="src" path="src"/>

<classpathentry kind="src" path="test"/>

即可。

如果使用命令行的方式,就要对编译和运行带有 package语句的java文件有一定的了解。首先建立上述文件结构,编写java源文件。其中第一个语句就是

package com.xyz;

编译HelloWorldTest.java 的时候要这样:

g:\test > javac –cp “../src; .” com/xyz/HelloWorldTest.java

运行则是:

g:\test > java –cp “../src; .” com.xyz.HelloWorldTest

其中的关键就是要设置好classpath。至于为什么要这样,网上资料非常多,这里就不再赘述了。

上一篇: 在Struts框架下大型Web应用程序的开发 刘正仁 下一篇: JAVA中异常使用范围的一些思考

http://www.west263.com 以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

来在学习使用 Junit test framwork .关于源代码和测试代码的组织问题我看了一些资料。其中有这样的建议: 
   1. Create test cases in the same package as the code under test. For example, the com.mydotcom.ecommerce package would contain all the application-level classes as well as the test cases for those components.

2.To avoid combining application and testing code in your source directories, create a mirrored directory structure aligned with the package structure that contains the test code.

(http://www.clarkware.com/articles/JUnitPrimer.html)

就是说测试代码应该被测试代码放在同一个package中,但是测试代码放在一个独立的目录中.也就是组织成下面这样:

1.     esrc
2.       com
3.         s xyz
4.              HelloWorld.java
5.      test
6.        com
7.           xyz
                                    HelloWorldTest.java

这样是比较合理。这样既可以方便地测试protected方法/域,同时不会导致源代码和测试代码混在一起,难于管理。

如果使用的是Eclipse,只需简单设置一下就可以了。
         project -> properties -> Source标签 addFolder 按钮。

或者直接在classpath文件中直接设置也可以。将

<classpathentry kind="src" path=" "/> 换成:

<classpathentry kind="src" path="src"/>

<classpathentry kind="src" path="test"/>

即可。


  如果使用命令行的方式,就要对编译和运行带有 package语句的java 文件有一定的了解。首先建立上述文件结构,编写java源文件。其中第一个语句就是

package com.xyz;

编译HelloWorldTest.java 的时候要这样:

   g:\test > javac –cp “../src; .” com/xyz/HelloWorldTest.java

运行则是:

   g:\test > java –cp “../src; .” com.xyz.HelloWorldTest

其中的关键就是要设置好classpath。至于为什么要这样,网上资料非常多,这里就不再赘述了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值