ant整合junit自動化測試,ant整合junit
一. 使用Junit進行測試
1. Java業務代碼:
public class HelloWorld {// 測試返回"world"public String hello() {return "world";}// 測試返回"hello"public String world() {return "hello";}// 測試為空白public String nil() {return null;}// 測試不為空白public String notNil() {return "abc";}// 測試拋出異常public String ext() {return null;}}
2. 使用junit3測試, 繼承TestCase
import junit.framework.TestCase;public class HelloWorldTest extends TestCase {private HelloWorld helloWorld; @Overrideprotected void setUp() throws Exception {helloWorld = new HelloWorld();System.out.println("helloWorld init");}public void testHello() {String str = helloWorld.hello();assertEquals("測試world失敗", str, "world");}public void testWorld() {String str = helloWorld.world();assertEquals("測試world失敗", str, "hello");}public void testNotNil() {assertNotNull("對象為空白", helloWorld.notNil());}public void testNil() {assertNull("對象不為空白", helloWorld.nil());}public void testExt() {try {helloWorld.ext();fail("沒有拋出異常");} catch (NumberFormatException e) {}}@Overrideprotected void tearDown() throws Exception {System.out.println("hello world destory");helloWorld = null;}}
4. 使用junit4測試, 使用註解
import org.junit.After;import org.junit.Before;import org.junit.Test;import static org.junit.Assert.*; // 靜態引入, assertEquals相容junit3public class HelloWorldTest {private HelloWorld helloWorld; @Beforepublic void setUp() {helloWorld = new HelloWorld();}@Testpublic void testHello() {String str = helloWorld.hello();assertEquals("hello測試失敗",str,"world");}@Testpublic void testWorld() {String str = helloWorld.world();assertEquals("world測試失敗",str, "hello");}@Testpublic void testNil() {assertNull("對象不為空白",helloWorld.nil());}@Testpublic void testNotNil() {assertNotNull("對象為空白", helloWorld.notNil());}@Test(expected=NumberFormatException.class)public void testExt() {helloWorld.ext();}@Afterpublic void tearDown() {helloWorld = null;}}
java ant怎測試Junit ?
你問的是 ant 怎麼調用/啟動junit 的測試類別嗎?
ant 執行junit測試的問題