junit单元测试工具在jbuider2005中的应用

王朝java/jsp·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

简单的框架

JUnit是由Erich Gamma和Kent Beck开发的开源测试框架,JBuilder集成了这个框架并对此做了扩展。JUnit之所以流行并为广大的开发人员所推崇,一是因为它实战性强,功能强大,二是因为它实在简单。一个产品或框架要能有生命力,最好都具备这样的特点。

简单地讲这个框架提供了许多断言(assert)方法,允许你设置测试的规则,如:assertEquals()、assertNull()、assertNotSame()、assertTrue()等方法,一个测试用例包括了多个断言,当运行测试用例后,JUnit运行器会报告哪些断言没有通过,开发人员就可顺藤摸瓜搞个水落石出了。而传统的测试方法需要将期望的结果用诸如System.out.println()等语句将过程信息打印到控制台或日志中,由开发人员观察输出信息以判断是否正确,现在这种"观察"的工作由JUnit的那些assertXxx()方法自动完成。

JUnit的测试框架类结构很简单,主要由3个类组成,其类图关系如下图所示:

图 JUnit测试框架类结构

·junit.framework.Test:测试接口。

·junit.framework.TestCase:测试用例类,业务类的测试用例类只需要承继这个TestCase,根据情况编写若干个public void testXxx()方法,在方法中通过assertYyy()定制若干测试规则就可以了。

·junit.framework.TestSuite:测试套件类,它可以将多个测试用例类捆绑在一起运行,也可以捆绑另一个测试套件。

在jbuider中使用junit

1、我们的简单java程序

package org.lyj.junit;

public class Test1 {

public Test1() {

}

public int getNum(int i){

return 1;

}

public String hello(String name){

return "hello,"+name;

}

}

package org.lyj.junit;

public class Test2 {

public Test2() {

}

public int getNum2(int i){

return 2;

}

public String getStr(){

return "test2";

}

}

2、由jbuider自动生成的testcase

package org.lyj.junit;

import junit.framework.*;

/**

* <p>Title: </p>

*

* <p>Description: </p>

*

* <p>Copyright: Copyright (c) 2005</p>

*

* <p>Company: </p>

*

* @author not attributable

* @version 1.0

*/

public class TestTest1 extends TestCase {

private Test1 test1 = null;

public TestTest1(String aa){

super(aa);

}

protected void setUp() throws Exception {

super.setUp();

test1 = new Test1();

}

protected void tearDown() throws Exception {

test1 = null;

super.tearDown();

}

public void testGetNum() {

int i = 0;

int expectedReturn = 1;

int actualReturn = test1.getNum(i);

assertEquals("return value", expectedReturn, actualReturn);

/**@todo fill in the test code*/

}

public void testHello() {

String name = "lyj";

String expectedReturn = "hello,lyj";

String actualReturn = test1.hello(name);

assertEquals("return value", expectedReturn, actualReturn);

/**@todo fill in the test code*/

}

}

package org.lyj.junit;

import junit.framework.*;

/**

* <p>Title: </p>

*

* <p>Description: </p>

*

* <p>Copyright: Copyright (c) 2005</p>

*

* <p>Company: </p>

*

* @author not attributable

* @version 1.0

*/

public class TestTest2 extends TestCase {

private Test2 test2 = null;

protected void setUp() throws Exception {

super.setUp();

test2 = new Test2();

}

protected void tearDown() throws Exception {

test2 = null;

super.tearDown();

}

public void testGetNum2() {

int i = 0;

int expectedReturn = 2;

int actualReturn = test2.getNum2(i);

assertEquals("return value", expectedReturn, actualReturn);

/**@todo fill in the test code*/

}

public void testGetStr() {

String expectedReturn = "test2";

String actualReturn = test2.getStr();

assertEquals("return value", expectedReturn, actualReturn);

/**@todo fill in the test code*/

}

}

3、生成测试集合

package org.lyj.junit;

import junit.framework.*;

/**

* <p>Title: </p>

*

* <p>Description: </p>

*

* <p>Copyright: Copyright (c) 2005</p>

*

* <p>Company: </p>

*

* @author not attributable

* @version 1.0

*/

public class TestSuite1 extends TestCase {

public TestSuite1(String s) {

super(s);

}

public static Test suite() {

TestSuite suite = new TestSuite();

suite.addTest(new TestTest1("testGetNum"));

suite.addTestSuite(TestTest2.class);

return suite;

}

}

package org.lyj.junit;

import junit.framework.*;

/**

* <p>Title: </p>

*

* <p>Description: </p>

*

* <p>Copyright: Copyright (c) 2005</p>

*

* <p>Company: </p>

*

* @author not attributable

* @version 1.0

*/

public class TestSuite3 extends TestCase {

public TestSuite3(String s) {

super(s);

}

public static Test suite() {

TestSuite suite = new TestSuite();

suite.addTestSuite(TestTest1.class);

suite.addTestSuite(TestTest2.class);

suite.addTest(TestSuite1.suite());

return suite;

}

}

也就是说我们在一个suit的里可以suite.addTestSuite(TestTest1.class)加入TestTest1.class里所有方法的测试用例,如果我们只加入其中的一个方法的测试可以注意TestSuite1 和TestTest1.class中红色字体中的实现。我们也可以通过suite.addTest(TestSuite1.suite())加入一个已经存在的测试用例集合。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航