TestNG Hello World Example

摘要: A classic example, show you how to get started with TestNG unit test framework.

A classic example, show you how to get started with TestNG unit test framework.

Tools used :

  1. TestNG 6.8.7
  2. Maven 3
  3. Eclipse IDE

1. TestNG Dependency

Add TestNG library in the pom.xml.

pom.xml
	<dependency>
		<groupId>org.testng</groupId>
		<artifactId>testng</artifactId>
		<version>6.8.7</version>
		<scope>test</scope>
	</dependency>	

2. TestNG Example

Review a simple class, has a method to return a fixed email “feedback@yoursite.com”.

RandomEmailGenerator.java
package com.mkyong.testng.project.service.email;
import org.springframework.stereotype.Service;
public class RandomEmailGenerator {
	public String generate() {
		return "feedback@yoursite.com";

Create a test case like this :

TestHelloWorld.java
package com.mkyong.testng.examples.helloworld;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.mkyong.testng.project.service.email.RandomEmailGenerator;
public class TestHelloWorld {
	@Test()
	public void testEmailGenerator() {
		RandomEmailGenerator obj = new RandomEmailGenerator();
		String email = obj.generate();
		Assert.assertNotNull(email);
		Assert.assertEquals(email, "feedback@yoursite.com");

Done, a simple TestNG test case is created, this test make sure the RandomEmailGenerator.generate() is always returns “feedback@yoursite.com”.

3. TestNG Eclipse Plug-In

To run above test in Eclipse IDE, you need to install TestNG Eclipse plug-in. Follow this official TestNG Eclipse plug-in guide for the installation.

To run TestNG test, right click on the test class and run as “TestNG Test”.

Result

References

  1. TestNG Official Documentation

上一篇: Java Custom Annotations Example
下一篇: Java Convert IP address to Decimal Number
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号