要在Maven里面使用TestNG很简单。去TestNG的网站上可以找到非常详细的一段javascript:;" onClick="javascript:tagshow(event, '代码');" target="_self">代码,将下列代码加入<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!");
}
}

  运行mvn test,一切正常。如果将sayHello()方法中的返回值改为"Hello World!"则报错。说明TestNG已经正常运行了。