在Maven里使用TestNG的方法的單元測試實例
要在Maven里面使用TestNG很簡單。去TestNG的網站上可以找到非常詳細的一段代碼,將下列代碼加入<dependencies></dependencies>標簽之間:
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.1.1</version> <scope>test</scope> </dependency> |
當然,這是對應的TestNG 6.1.1版本。其它版本的TestNG,應當只需要改動一下版本號即可。
在src/main/java文件夾中書寫Main.java文件如下:
package org.silentmusicbox.justanothermavenproject; public class Main { public String sayHello() { return "Hallo Welt!"; } public static void main(String [] args) { Main objOfMain = new Main(); System.out.println(objOfMain.sayHello()); } } |
在src/test/java文件夾中書寫TestMain.java文件如下:
packageorg.silentmusicbox.justanothermavenproject; importorg.testng.Assert; importorg.testng.annotations.BeforeMethod; importorg.testng.annotations.Test; publicclassTestMain{ privateMainm; @BeforeMethod publicvoidinit(){ m=newMain(); } @Test publicvoidtestSayHello(){ Assert.assertEquals(m.sayHello(),"HalloWelt!"); } } |