欢迎光临
我们一直在努力

Eclipse学习4-在Eclipse中使用JUnit进行单元测试(下)-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

使用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 its 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。(全文完)

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Eclipse学习4-在Eclipse中使用JUnit进行单元测试(下)-JSP教程,Java技巧及代码
分享到: 更多 (0)

相关推荐

  • 暂无文章