Ant and TestNG Task example

In this tutorial, we will show you how to run a TestNG test in Ant build. 1. Run by Classes build.xml <taskdef name="testng" classname="org.testng.TestNGAntTask"> <classpath location="lib/testng-6.8.14.jar" /> </taskdef> <target name="testng" depends="compile"> <!– Assume test.path contains the project library dependencies –> <testng classpathref="test.path" outputDir="${report.dir}" haltOnFailure="true"> <!– Extra project classpath, which is not included in above "test.path" …

Read more

TestNG + Spring Integration Example

In this tutorial, we will show you how to test Spring’s components with TestNG. Tools used : TestNG 6.8.7 Spring 3.2.2.RELEASE Maven 3 Eclipse IDE 1. Project Dependencies To integrate Spring with TestNG, you need spring-test.jar, add the following : pom.xml <properties> <spring.version>3.2.2.RELEASE</spring.version> <testng.version>6.8.7</testng.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> …

Read more

TestNG – Groups Test

In this tutorial, we will show you how to do the group testing in TestNG. 1. Groups on Methods Review a test group example. runSelenium() and runSelenium1() are belong to group selenium-test. testConnectOracle() and testConnectMsSQL() are belong to group database. runFinal() will be executed if groups selenium-test and database are passed. TestGroup.java package com.mkyong.testng.examples.group; import …

Read more

TestNG Hello World Example

A classic example, show you how to get started with TestNG unit test framework. Tools used : TestNG 6.8.7 Maven 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 “[email protected]”. …

Read more

Spring Batch unit test example

In this tutorial, we will show you how to unit test Spring batch jobs with jUnit and TestNG frameworks. To unit test batch job, declares spring-batch-test.jar, @autowired the JobLauncherTestUtils, launch the job or step, and assert the execution status. 1. Unit Test Dependencies To unit test Spring batch, declares following dependencies : pom.xml <!– Spring …

Read more

TestNG Tutorial

TestNG tutorial with full example, introduce the TestNG basic usage, annotation support, and test case for expected exception test, ignore test, time test, suite test, parameterized test, dependency test and etc

TestNG parameter testing example

Yet another TestNG parameter testing example, with @DataProvider. 1. CharUtil Class Let’s say, a class to convert a character to ASCII or vice verse, how can you unit test it with TestNG? CharUtils.java package com.mkyong.testng.examples.parameter; /** * Character Utility class * * @author mkyong * */ public class CharUtils { /** * Convert the characters …

Read more

JUnit 4 Vs TestNG – Comparison

JUnit 4 and TestNG are both very popular unit test framework in Java. Both frameworks look very similar in functionality. Which one is better? Which unit test framework should I use in Java project? Here I did a feature comparison between JUnit 4 and TestNG. 1. Annotation Support The annotation supports are implemented in both …

Read more

TestNG – Dependency Test

In TestNG, we use dependOnMethods and dependsOnGroups to implement dependency testing. If a dependent method is fail, all the subsequent test methods will be skipped, NOT failed. 1. dependOnMethods Example A simple example, “method2()” is dependent on “method1()”. 1.1 If method1() is passed, method2() will be executed. App.java package com.mkyong.testng.examples.dependency; import org.testng.annotations.Test; public class App …

Read more

TestNG – Parameter Test (XML and @DataProvider)

In this tutorial, we will show you how to pass parameters into a @Test method, via XML @Parameters or @DataProvider. 1. Passing Parameters with XML In this example, the properties filename is passing from testng.xml, and inject into the method via @Parameters. TestParameterXML.java package com.mkyong.testng.examples.parameter; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties; …

Read more

TestNG – Run multiple test classes (suite test)

In this tutorial, we will show you how to run multiple TestNG test cases (classes) together, aka suite test. 1. Test Classes Review following three test classes. TestConfig.java package com.mkyong.testng.examples.suite; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; //show the use of @BeforeSuite and @BeforeTest public class TestConfig { @BeforeSuite public void testBeforeSuite() { System.out.println("testBeforeSuite()"); …

Read more

TestNG – Timeout Test

In this tutorial, we will show you how to perform timeout test in TestNG. The “timeout” means if a unit test is taking longer than a specified number of milliseconds to finish, TestNG will abort it and market it as failed. This “timeout” can also use for performance test, to ensure the method is returned …

Read more

TestNG – How to ignore a test method

In this tutorial, we will show you how to ignore a test method with @Test(enabled = false). TestIgnore.java package com.mkyong.testng.examples.ignore; import org.testng.Assert; import org.testng.annotations.Test; public class TestIgnore { @Test //default enable=true public void test1() { Assert.assertEquals(true, true); } @Test(enabled = true) public void test2() { Assert.assertEquals(true, true); } @Test(enabled = false) public void test3() { …

Read more

TestNG – Expected Exception Test

In this tutorial, we will show you how to use the TestNG expectedExceptions to test the expected exception throws in your code. 1. Runtime Exception This example shows you how to test a runtime exception. If the method divisionWithException () throws a runtime exception – ArithmeticException, it will be passed. TestRuntime.java package com.mkyong.testng.examples.exception; import org.testng.annotations.Test; …

Read more