Eclipse学习4-在Eclipse中使用JUnit进行单元测试(下)

来源:互联网 发布:automator是什么软件 编辑:程序博客网 时间:2024/05/16 15:46

使用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。另外选择setUptearDown方法,这些方法建立和清理在测试用例中的数据和(或者)对象(它们在JUnit中的术语为fixtures)。按照上图填好后,点击Next。如图:

在此对话框中,选择你想要测试的方法,这样JUnit Wizard能够为它们创建存根(stub)。因为我们想要测试allocate,seeget,选择它们,并点击Finish按钮来创建JN_testTest class。如下所示:在JN_testTest class中为allocatesetget方法都创建了一个方法存根: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类中的allocatesetget方法,这样就能对结果使用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,现在只有testGettestSet两处错误,如图:

testGettestSet出了什么问题?这里的问题是所有的JUnit测试都是单独运行的。也就是说尽管第一个测试testAllocate调用了allocate方法,但是它并没有被接下来的两个测试testGettestSet调用。所以为了给这两个测试初始化其数组,必须在testGettestSet中都调用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。(全文完)

原创粉丝点击