|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
使用JUnit测试一个应用程序 现在已经预备好测试JN_test应用程序。为了测试,还需要使用JUnit Wizard创建一个新的class来扩展JUnit测试用例。要使用此wizard,请在Package Explorer 中的JN_test上单击右键,并且选择New->Other来打开一个New对话框,如图所示: 现在展开Java结点并选择JUnit,然后再选择JUnit Test Case,单击Next按钮,如图: 通常情况下JUnit类命名要和被它测试的类同名,并在其后面添加Test。所以命名为JN_testTest。另外选择setUp和tearDown方式,这些方式建立和清理在测试用例中的数据和(或者)对象(它们在JUnit中的术语为fixtures)。按照上图填好后,点击Next。如图: 在此对话框中,选择你想要测试的方式,这样JUnit Wizard能够为它们创建存根(stub)。因为我们想要测试allocate,see和get,选择它们,并点击Finish按钮来创建JN_testTest class。如下所示:在JN_testTest class中为allocate,set和get方式都创建了一个方式存根:testAllocate, testSet, 和 testGet。 package net.csdn.blog; import junit.framework.TestCase; public class JN_testTest extends TestCase { /* * @see TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); } /* * @see TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); } public void testAllocate() { } public void testGet() { } public void testSet() { } } 下一步就是在这些存根中添加代码,以让它们来调用JN_test类中的allocate,set和get方式,这样就能对结果使用JUnit断言方式。我们将需要一个JN_test类的一个对象来调用这些方式,将其命名为testObject。要创建testObject使用JUnit代码中的setUp方式。此方式在JUnit测试开始之前就被调用,这样我们将从将要测试的JN_test类中创建testObject。 JN_test testObject; . . . protected void setUp() throws Exception { super.setUp(); testObject = new JN_test(); } 现在就可以用这个对象来进行测试了。比如,allocate方式是用来创建一个整型数组并返回此数组的,所以在testAllocate中使用assertNotNull测试并确信此数组不为空: public void testAllocate() { assertNotNull(testObject.allocate()); } The get method is supposed to retrieve a value from the array, so we can test that method using assertEquals with a test value in testGet: get方式从数组中取得数值,在testGet中用assertEquals来测试此方式。 public void testGet() { assertEquals(testObject.get(1),1); } And the set method is supposed to return true if it\\\'s been successful, so we can test it with assertTrue like this set方式在成功的时候返回true,使用assertTrue来测试它。 public void testSet() { assertTrue(testObject.set(2,4)); } 在添加代码后,选择Package Explorer 中的JN_testTest,并选择Run As->JUnit Test 菜单项,如图所示: 上面的图示说明这里有错误,在JUnit视图中,三个测试都被打上了叉,表示测试失败,我们先检查第一个测试,testAllocate,它测试的是被创建的数组不能为null: public void testAllocate( ) { assertNotNull(testObject.allocate( )); } 看看第一个trace:哦,这个地方遗漏了创建数组,我们修正代码: public int[] allocate() { array = new int[3]; array[0] = 0; array[1] = 1; array[2] = 2; return array; } 现在再运行JN_testTest,现在只有testGet和testSet两处错误,如图: testGet和testSet出了什么问题?这里的问题是所有的JUnit测试都是单独运行的。也就是说尽管第一个测试testAllocate调用了allocate方式,但是它并没有被接下来的两个测试testGet和testSet调用。所以为了给这两个测试初始化其数组,必须在testGet和testSet中都调用allocate方式。添加以下代码: public void testGet() { testObject.allocate(); assertEquals(testObject.get(1),1); } public void testSet() { testObject.allocate(); assertTrue(testObject.set(2,4)); } 现在运行测试,全部都通过。JUnit视图中顶部的菜单栏为绿色,如图所示: 如上所示,JUnit提供了相称简朴的方法来创建一组标准的测试,我们只需要动几下鼠标就可以实现。只要测试被创建,你只需要运行你创建的JUnit类。 然而,需要注重的是JUnit只是用一组测试来检验兼容性,假如你的代码中存在问题,或者你不知道怎么办,这时你需要进行debug。(全文完) 返回类别: 教程 上一教程: JDBC常见问题 -来自sun论坛 下一教程: J2ME学习??从WTK到厂商SDK 您可以阅读与"Eclipse学习4-在Eclipse中使用JUnit进行单元测试(下)"相关的教程: · Eclipse学习4-在Eclipse中使用JUnit进行单元测试(上) · JUnit和单元测试入门简介 · 在Eclipse中使用SWT进行界面设计 · 在eclipse中使用checkstyle插件自动检查编码规范 · 构建一个轻易单元测试的java--web系统 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |