//package justnorth; import junit.framework.*; import junit.textui.*;public class MyTest extends TestCase { //standard constructor public MyTest(String name) { super(name); } public void testTemp() throws Exception { codein=new code("yello"); codein.yourmethod(); } /** * Returns all tests which should be performed for testing class. * By default it returns only name of testing class. Instance of this * is then created with its constructor. */ public static Test suite() { /* Junit uses reflexion to add automaticallu all the methods od TestFoo whose name begins by "test" */ return new TestSuite(MyTest.class); } /** * This main method is used for run tests for this class only * from command line. */ public static void main(String[] args) { /* to user command line interface */ junit.textui.TestRunner.run(suite()); } }
import junit.framework.*;
import junit.textui.*;public class MyTest extends TestCase
{
//standard constructor
public MyTest(String name) {
super(name);
}
public void testTemp()
throws Exception
{
codein=new code("yello");
codein.yourmethod();
}
/**
* Returns all tests which should be performed for testing class.
* By default it returns only name of testing class. Instance of this
* is then created with its constructor.
*/
public static Test suite() {
/* Junit uses reflexion to add automaticallu all the methods od TestFoo
whose name begins by "test"
*/
return new TestSuite(MyTest.class);
} /**
* This main method is used for run tests for this class only
* from command line.
*/
public static void main(String[] args) { /* to user command line interface */
junit.textui.TestRunner.run(suite()); }
}
2) 重载runTest() 方法。
3) 用assert…()进行判断。
主要包括: assert(), assertEquals(), assertNull(), assertSame(), assertTrue(),例如:
我们要测试的类是:(见:附源程序)由于此类没有Main方法不能被执行,所以需要用Junit 对他进行测试。
编写测试文件如下:(见:附源程序)
说明:
1)setUp(),tearDown(),suite()是Junit中自有的方法。可以直接覆盖
2)测试方法的开头应该使用默认的小写test + 测试方法名。
3)Suite的构造方法:
4)下面是几个需要注意的事项:1. 不要用TestCase的构造函数初始化Fixture,而要用setUp()和tearDown()方法。
2. 不要依赖或假定测试运行的顺序,因为JUnit利用Vector保存测试方法。所以不同的平台会按不同的顺序从Vector中取出测试方法。
3. 避免编写有副作用的TestCase。例如:如果随后的测试依赖于某些特定的交易数据,就不要提交交易数据。简单的回就可以了。
4. 当继承一个测试类时,记得调用父类的setUp()和tearDown()方法。
5. 将测试代码和工作代码放在一起,一边同步编译和更新。(使用Ant中有支持junit的task.)
6. 测试类和测试方法应该有一致的命名方案。如在工作类名前加上test从而形成测试类名。7. 确保测试与时间无关,不要依赖使用过期的数据进行测试。导致在随后的维护过程中很难重现测试。
7. 如果你编写的软件面向国际市场,编写测试时要考虑国际化的因素。不要仅用母语的Locale进行测试。
9. 尽可能地利用JUnit提供地assert/fail方法以及异常处理的方法,可以使代码更为简洁。10.测试要尽可能地小,执行速度快。三、运行测试类:
1) 先用Javac编译上面写好的测试类。Javac MytestCar.class2) 用 Java junit.textui.TestRunner MytestCar 运行测试 (命令行形式)
或用Java junit.awtui.TestRunner MytestCar运行测试 (awt窗口形式不推荐使用)
或用Java junit.swingui.TestRunner MytestCar运行测试 (swing窗口形式)
(从窗口形式可以更好的运行测试可查测试结果)
还有一个问题需要注意:如果JUnit报告了测试没有成功,JUnit会区分失败(failures)和错误(errors)。
失败:是一个期望值被assert方法测试后的结果。
错误:是异常引起的,如ArrayIndexOutOfBoundsException。四、 评价测试结果测试完成后返回的画面说明附源程序:名词解释:TestSuite:
suite()方法就像main()方法一样,JUnit用它来执行测试。在suite()方法中,你将测试实例加到一个TestSuite对象中,并返回这个TestSuite对象。一个TestSuite对象可以运行一组测试。TestSuite和TestCase都实现了Test接口(interface),而Test接口定义了运行测试所需的方法。这就允许你用TestCase和TestSuite的组合创建一个TestSuite。这就是为什么我们前面说TestCase,TestSuite以及TestSuite组成了一个composite。JUnit的局限性及解决方法
比如对图形界面的测试,对servlet/JSP以及EJB的测试我们都没有举相关的例子。实际上,JUnit对于GUI界面,servlet/JSP,JavaBean以及EJB都有办法测试。前面我们所做的测试实际上有一个隐含的环境,JVM我们的类需要这个JVM来执行。而在J2EE框架中,servlet/JSP,EJB都要求有自己的运行环境:Web Container和EJB Container。所以,要想对servlet/JSP,EJB进行测试就需要将其部署在相应的Container中才能进行测试。由于EJB不涉及UI的问题(除非EJB操作XML数据,此时的测试代码比较难写,有可能需要你比较两棵DOM树是否含有相同的内容)只要部署上去之后就可以运行测试代码了。此时setUp()方法显得特别有用,你可以在setUp()方法中利用JNDI查找特定的EJB。而在testXXX()方法中调用并测试这些EJB的方法。
这里所指的JavaBean同样没有UI的问题,比如,我们用JavaBean来访问数据库,或用JavaBean来包裹EJB。如果这类JavaBean没有用到Container的提供的服务,则可直接进行测试,同我们前面所说的一般的类的测试方法一样。如果这类JavaBean用到了Container的提供的服务,则需要将其部署在Container中才能进行测试。方法与EJB类似。 对于servlet/JSP的测试则比较棘手,有人建议在测试代码中构造HttpRequest和HttpResponse,然后进行比较,这就要求开发人员对HTTP协议以及servlet/JSP的内部实现有比较深的认识。我认为这招不太现实。也有人提出使用HttpUnit。由于我对Cactus和HttpUnit 了解不多,所以无法做出合适的建议。
正是由于JUnit的开放性和简单易行,才会引出这篇介绍文章。但技术总在不断地更新,而且我对测试并没有非常深入的理解;我可以将一个复杂的概念简化成一句非常容易理解的话。但我的本意只是希望能降低开发人员步入测试领域的门槛,而不是要修改或重新定义一些概念。这一点是特别要强调的。最后,如果指出一些注意事项或我对某些问题的理解有误,我会非常感激的。