我們先看一下在JUnit 3中我們是怎樣寫一個(gè)單元測(cè)試的。比如下面一個(gè)類: public class AddOperation { public int add(int x,int y){ return x+y; } }
我們要測(cè)試add這個(gè)方法,我們寫單元測(cè)試得這么寫: import junit.framework.TestCase; import static org.junit.Assert.*; public class AddOperationTest extends TestCase{
public void setUp() throws Exception { }
public void tearDown() throws Exception { }
public void testAdd() { System.out.println(/"add/"); int x = 0; int y = 0; AddOperation instance = new AddOperation(); int expResult = 0; int result = instance.add(x, y); assertEquals(expResult, result); } }
/** * * @author bean */ public class AddOperationTest extends TestCase{
public AddOperationTest() { }
@Before public void setUp() throws Exception { }
@After public void tearDown() throws Exception { }
@Test public void add() { System.out.println(/"add/"); int x = 0; int y = 0; AddOperation instance = new AddOperation(); int expResult = 0; int result = instance.add(x, y); assertEquals(expResult, result); }