Getting Start with Junit
Junit是一个轻量级测试框架,具有良好的扩展性且容易上手,你可以写一个测试并运行查看结果是否与期望相符
1. 一个测试用例用来执行若干测试。定义一个测试用例:
(1)实现一个TestCase的子类
(2)覆盖 setUp 方法,setUp在测试之前被调用,创建测试对象,建立网络,数据库联接等
(3)定义一系列 testXXX 方法
(4)覆盖 tearDown 方法(可选),tearDown在测试之后被调用,关闭建立的资源
(5)运行测试
看个例子:
[code]
import junit.framework.*;
import junit.swingui.TestRunner;
import java.util.*;
public class HashMapTest extends TestCase {
private Map map;
/**
* 创建一个 HashMap 用来测试
*/
protected void setUp() throws Exception {
map = new HashMap();
map.put( "larry","oracle" );
map.put( "bill", "microsoft" );
}
/**
* 定义测试方法,断言预期结果
*/
public void testRemove() throws Exception {
map.remove( "bill" );
assertEquals( 1, map.size() );
}
public void testCount() throws Exception {
assertEquals( 2, map.size() );
}
public void testAdd() throws Exception {
map.put( "irini", "dell" );
assertEquals( 3, map.size() );
}
public void testKeyIndex() throws Exception {
assertEquals( map.get( "larry" ), "oracle" );
assertNull( map.get( "dueshu" ) );
}
public void tearDown() throws Exception {
}
public static void main( String[] args ){
TestRunner.run( HashMapTest.class );
}
}
[/code]
在来看一个Junit自带的例子:
import junit.framework.*;
import java.util.Vector;
/**
* A sample test case, testing <code>java.util.Vector</code>.
*
*/
public class VectorTest extends TestCase {
protected Vector fEmpty;
protected Vector fFull;
public static void main (String[] args) {
junit.textui.TestRunner.run (suite());
}
protected void setUp() {
fEmpty= new Vector();
fFull= new Vector();
fFull.addElement(new Integer(1));
fFull.addElement(new Integer(2));
fFull.addElement(new Integer(3));
}
public static Test suite() {
return new TestSuite(VectorTest.class);
}
public void testCapacity() {
int size= fFull.size();
for (int i= 0; i < 100; i++)
fFull.addElement(new Integer(i));
assertTrue(fFull.size() == 100+size);
}
public void testClone() {
Vector clone= (Vector)fFull.clone();
assertTrue(clone.size() == fFull.size());
assertTrue(clone.contains(new Integer(1)));
}
public void testContains() {
assertTrue(fFull.contains(new Integer(1)));
assertTrue(!fEmpty.contains(new Integer(1)));
}
public void testElementAt() {
Integer i= (Integer)fFull.elementAt(0);
assertTrue(i.intValue() == 1);
try {
fFull.elementAt(fFull.size());
} catch (ArrayIndexOutOfBoundsException e) {
return;
}
fail("Should raise an ArrayIndexOutOfBoundsException");
}
public void testRemoveAll() {
fFull.removeAllElements();
fEmpty.removeAllElements();
assertTrue(fFull.isEmpty());
assertTrue(fEmpty.isEmpty());
}
public void testRemoveElement() {
fFull.removeElement(new Integer(3));
assertTrue(!fFull.contains(new Integer(3)) );
}
}
2. 组测试
先看例子:
import junit.framework.*;
public class AllTests {
public static void main( String[] args ){
junit.swingui.TestRunner.run( AllTests.class );
}
public static Test suite() {
TestSuite suit = new TestSuite( "a sample suit test" );
suit.addTest( new TestSuite( HashMapTest.class ) );
suit.addTest( new TestSuite( VectorTest.class ) );
return suit;
}
}
suite方法把HashMapTest中所有的测试方法和VectorTest中所有的测试方法组织在一起进行测试
1. 一个测试用例用来执行若干测试。定义一个测试用例:
(1)实现一个TestCase的子类
(2)覆盖 setUp 方法,setUp在测试之前被调用,创建测试对象,建立网络,数据库联接等
(3)定义一系列 testXXX 方法
(4)覆盖 tearDown 方法(可选),tearDown在测试之后被调用,关闭建立的资源
(5)运行测试
看个例子:
[code]
import junit.framework.*;
import junit.swingui.TestRunner;
import java.util.*;
public class HashMapTest extends TestCase {
private Map map;
/**
* 创建一个 HashMap 用来测试
*/
protected void setUp() throws Exception {
map = new HashMap();
map.put( "larry","oracle" );
map.put( "bill", "microsoft" );
}
/**
* 定义测试方法,断言预期结果
*/
public void testRemove() throws Exception {
map.remove( "bill" );
assertEquals( 1, map.size() );
}
public void testCount() throws Exception {
assertEquals( 2, map.size() );
}
public void testAdd() throws Exception {
map.put( "irini", "dell" );
assertEquals( 3, map.size() );
}
public void testKeyIndex() throws Exception {
assertEquals( map.get( "larry" ), "oracle" );
assertNull( map.get( "dueshu" ) );
}
public void tearDown() throws Exception {
}
public static void main( String[] args ){
TestRunner.run( HashMapTest.class );
}
}
[/code]
在来看一个Junit自带的例子:
import junit.framework.*;
import java.util.Vector;
/**
* A sample test case, testing <code>java.util.Vector</code>.
*
*/
public class VectorTest extends TestCase {
protected Vector fEmpty;
protected Vector fFull;
public static void main (String[] args) {
junit.textui.TestRunner.run (suite());
}
protected void setUp() {
fEmpty= new Vector();
fFull= new Vector();
fFull.addElement(new Integer(1));
fFull.addElement(new Integer(2));
fFull.addElement(new Integer(3));
}
public static Test suite() {
return new TestSuite(VectorTest.class);
}
public void testCapacity() {
int size= fFull.size();
for (int i= 0; i < 100; i++)
fFull.addElement(new Integer(i));
assertTrue(fFull.size() == 100+size);
}
public void testClone() {
Vector clone= (Vector)fFull.clone();
assertTrue(clone.size() == fFull.size());
assertTrue(clone.contains(new Integer(1)));
}
public void testContains() {
assertTrue(fFull.contains(new Integer(1)));
assertTrue(!fEmpty.contains(new Integer(1)));
}
public void testElementAt() {
Integer i= (Integer)fFull.elementAt(0);
assertTrue(i.intValue() == 1);
try {
fFull.elementAt(fFull.size());
} catch (ArrayIndexOutOfBoundsException e) {
return;
}
fail("Should raise an ArrayIndexOutOfBoundsException");
}
public void testRemoveAll() {
fFull.removeAllElements();
fEmpty.removeAllElements();
assertTrue(fFull.isEmpty());
assertTrue(fEmpty.isEmpty());
}
public void testRemoveElement() {
fFull.removeElement(new Integer(3));
assertTrue(!fFull.contains(new Integer(3)) );
}
}
2. 组测试
先看例子:
import junit.framework.*;
public class AllTests {
public static void main( String[] args ){
junit.swingui.TestRunner.run( AllTests.class );
}
public static Test suite() {
TestSuite suit = new TestSuite( "a sample suit test" );
suit.addTest( new TestSuite( HashMapTest.class ) );
suit.addTest( new TestSuite( VectorTest.class ) );
return suit;
}
}
suite方法把HashMapTest中所有的测试方法和VectorTest中所有的测试方法组织在一起进行测试
irini
2005-12-11 23:18:57
评论:0
阅读:288
引用:0
