Junit Testeclipse
1.在eclipse中新建一個功能爲hello world 的項目,命名爲「han_ven72」maven
2.根據教程將JUnit4單元測試包引入了這個項目單元測試
3.分別新建一個helloworld類和test類,分別命名爲「ven」和「venTest」測試
p.s 按照正常順序起個名字一直點就好。。ui
4.分別向「ven」和「venTest」中編寫代碼spa
ven:3d
public class ven { public String sayHello(){ return "Hello han_ven72"; } public static void main(String args[]) { ven v = new ven(); System.out.println(v.sayHello()); } }
venTest:code
1 import junit.framework.TestCase; 2 3 public class venTest extends TestCase { 4 public venTest(String name){ 5 super(name); 6 } 7 public static void main(String args[]){ 8 junit.textui.TestRunner.run(venTest.class); 9 } 10 public void testsayHello(){ 11 ven v=new ven(); 12 assertEquals("Hello han_ven72", v.sayHello()); 13 } 14 }
5.將venTest Run As JUnit Testblog
Maven Test教程
1.根據教程下載安裝maven
2.配環境
3.在eclipse中新建 Maven 項目
4.分別在「App」,"AppTest"中編寫代碼
App:
1 package maven.maven1; 2 3 /** 4 * Hello world! 5 * 6 */ 7 public class App 8 { 9 public String sayHello(){ 10 return "Hello han_ven72"; 11 } 12 public static void main( String[] args ) 13 { 14 App v = new App(); 15 System.out.println(v.sayHello()); 16 } 17 }
AppTest:
1 package maven.maven1; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 7 /** 8 * Unit test for simple App. 9 */ 10 public class AppTest 11 extends TestCase 12 { 13 /** 14 * Create the test case 15 * 16 * @param testName name of the test case 17 */ 18 public AppTest( String testName ) 19 { 20 super( testName ); 21 } 22 23 /** 24 * @return the suite of tests being tested 25 */ 26 public static Test suite() 27 { 28 return new TestSuite( AppTest.class ); 29 } 30 31 /** 32 * Rigourous Test :-) 33 */ 34 public void testApp() 35 { 36 assertTrue( true ); 37 } 38 39 public void testsayHello(){ 40 App n = new App(); 41 String result = n.sayHello(); 42 assertEquals("Hello han_ven72",result); 43 } 44 }
5.將Maven Run As JUnit Test
以上步驟即在教程和大神的幫助下完成的Junit Test AND Maven Test