SlideShare a Scribd company logo
Java. Unit and
Integration Testing
IT Academy
22/07/2014
Agenda
▪Unit and Integration Testing
▪JUnit
▪TestNG
▪Parallel Tests Running
▪Case studies
Testing Types. Unit Testing
▪ Unit testing is a procedure used to validate that individual
units of source code are working properly.
▪ The goal of unit testing is to isolate each part of the program
and show that the individual parts are correct.
Integration Testing
▪ Integration testing is the phase of software testing in which
individual software modules are combined and tested as a
group.
▪ This is testing two or more modules or functions together with
the intent of finding interface defects between the modules or
functions.
Integration Testing
▪ Task: Database scripts, application main code and GUI
components were developed by different programmers. There
is need to test the possibility to use these 3 parts as one
system.
▪ Integration Testing Procedure: Combine 3 parts into one
system and verify interfaces (check interaction with database
and GUI).
▪ Defect: “Materials” button action returns not list of books but
list of available courses.
Unit / Integration Testing
public class One {
private String text;
public One() {
// Code
}
// Functionality
public int calc() {
int result = 0;
// Code
return result;
}
// get, set, etc.
}
Unit / Integration Testing
public class Two {
private One one;
public Two(One one) {
this.one = one;
}
public String resume() {
// Functionality
return one.getText() + "_" + one.calc().toString();
}
// Code
}
Unit / Integration Testing
public class Appl {
public static void main(String[] args) {
One one = new One();
one.setText("data“);
Two two = new Two(one);
two.resume();
// etc
}
}
Unit / Integration Testing
public class TwoTest {
@Test
public void TestResume() {
One one = new One();
one.setText("Integration Test“);
Two two = new Two(one);
// TODO Initialize to an appropriate value
String expected = "Integration Test_..."; // Result ???
String actual;
actual = two.resume();
Assert.AreEqual(expected, actual);
} }
Unit / Integration Testing
▪ A method stub or simply stub in software development is a
piece of code used to stand in for some other programming
functionality.
▪ A stub may simulate the behavior of existing code or be a
temporary substitute for yet-to-be-developed code.
public interface IOne {
void setText(String text);
String getText();
int calc();
}
Unit / Integration Testing
public class One implements IOne {
private String text;
public One() {
// Code
}
// Functionality
public int calc() {
int result = 0;
// Code
return result;
}
// get, set, etc.
}
Unit / Integration Testing
public class Two {
private IOne one;
public Two(IOne one) {
this.one = one;
}
public String resume() {
// Functionality
return one.getText() + "_" + one.calc().toString();
}
// Code
}
Unit / Integration Testing
▪ Stub – a piece of code used to stand in for some other
programming functionality.
public class StubOne extends IOne {
private String text;
public StubOne() {
}
public void setText(String text) {
}
public String getText() {
return “”
}
public int Calc() {
return 0;
}
}
Unit / Integration Testing
public class TwoTest {
@Test
public void TestResume() {
IOne one = new StubOne();
one.setText(" Unit Test“);
Two two = new Two(one);
// TODO Initialize to an appropriate value
String expected = " Unit Test_..."; // Result !!!
String actual;
actual = two.resume();
Assert.AreEqual(expected, actual);
} }
Junit 3
import junit.framework.TestCase;
рublic class AddJavaTest extends TestCase {
protected void setUp() throws Exception {
// create object }
protected void tearDown() throws Exception {
// to free resources }
public AddJavaTest(String name){
super (name); }
public void testSimpleAddition( ){
assertTrue(expect == actual);
} }
▪deprecated
JUnit
Assert class contains many different overloaded methods.
http://www.junit.org/ https
://github.com/kentbeck/junit/wiki
assertEquals(long expected, long actual)
Asserts that two longs are equal.
assertTrue(boolean condition)
Asserts that a condition is true.
assertFalse(boolean condition)
Asserts that a condition is false.
assertNotNull(java.lang.Object object)
Asserts that an object isn't null.
assertNull(java.lang.Object object)
Asserts that an object is null.
JUnit
assertSame(java.lang.Object expected,
java.lang.Object actual)
Asserts that two objects refer to the same object.
assertNotSame(java.lang.Object unexpected,
java.lang.Object actual)
Asserts that two objects do not refer to the same object.
assertArrayEquals(byte[] expecteds,
byte[] actuals)
Asserts that two byte arrays are equal.
fail()
Fails a test with no message.
fail(java.lang.String message)
Fails a test with the given message.
Junit 4
import org.junit.Test;
import org.junit.Assert;
public class MathTest {
@Test
public void testEquals( ) {
Assert.assertEquals(4, 2 + 2);
Assert.assertTrue(4 == 2 + 2);
}
@Test
public void testNotEquals( ) {
Assert.assertFalse(5 == 2 + 2);
} }
JUnit
JUnit
JUnit
JUnit
TestNG
▪ JUnit 4 and TestNG are both very popular unit test framework
in Java.
▪ TestNG (Next Generation) is a testing framework which
inspired by JUnit and NUnit, but introducing many new
innovative functionality like dependency testing, grouping
concept to make testing more powerful and easier to do. It is
designed to cover all categories of tests: unit, functional, end-
to-end, integration, etc.
TestNG
▪ Setting up TestNG with Eclipse.
▪ Click Help –> Install New Software
▪ Type http://beust.com/eclipse in the "Work with" edit box and
click ‘Add’ button.
▪ Click Next and click on the radio button "I accept the terms of
the license agreement“.
▪ After the installation, it will ask for restart of Eclipse. Then
restart the Eclipse.
▪ Once the Eclipse is restarted, we can see the TestNG icons &
menu items as in the below figures.
TestNG
▪ To create a new TestNG class, select the menu
File/New/TestNG
TestNG
▪ The next page lets you specify where that file will be created
TestNG
▪ Basic usage
import java.util.*;
import org.testng.Assert;
import org.testng.annotations.*;
public class TestNGTest1 {
private Collection collection;
@BeforeClass
public void oneTimeSetUp() {
System.out.println("@BeforeClass - oneTimeSetUp"); }
@AfterClass
public void oneTimeTearDown() {
// one-time cleanup code
System.out.println("@AfterClass - oneTimeTearDown"); }
TestNG
@BeforeMethod
public void setUp() {
collection = new ArrayList();
System.out.println("@BeforeMethod - setUp");
}
@AfterMethod
public void tearDown() {
collection.clear();
System.out.println("@AfterMethod - tearDown");
}
TestNG
@Test
public void testEmptyCollection() {
Assert.assertEquals(collection.isEmpty(),true);
System.out.println("@Test - testEmptyCollection");
}
@Test
public void testOneItemCollection() {
collection.add("itemA");
Assert.assertEquals(collection.size(),1);
System.out.println("@Test - testOneItemCollection");
}
}
TestNG
▪ Expected Exception Test
▪ The divisionWithException() method will throw an
ArithmeticException Exception, since this is an expected
exception, so the unit test will pass.
import org.testng.annotations.*;
public class TestNGTest2 {
@Test(expectedExceptions = ArithmeticException.class)
public void divisionWithException() {
int i = 1/0;
}
}
TestNG
▪ Suite Test. The “Suite Test” means bundle a few unit test cases
and run it together.
▪ In TestNG, XML file is use to define the suite test.
<!DOCTYPE suite SYSTEM
"http://beust.com/testng/testng-1.0.dtd" >
<suite name="My test suite">
<test name="testing">
<classes>
<class name="TestNGTest1" />
<class name="TestNGTest2" />
</classes>
</test>
</suite>
▪"TestNGTest1" and "TestNGTest2" will
execute together
TestNG
▪ Parameterized Test
▪ In TestNG, XML file or “@DataProvider” is used to provide
vary parameter for unit testing.
▪ Declare “@Parameters” annotation in method which needs
parameter testing, the parametric data will be provide by
TestNG’s XML configuration files. By doing this, you can reuse
a single test case with different data sets easily.
import org.testng.annotations.*;
public class TestNGTest6 {
@Test
@Parameters(value="number")
public void parameterIntTest(int number) {
System.out.println("Parameterized Number is: " +
number);
} }
TestNG
<!DOCTYPE suite SYSTEM
"http://beust.com/testng/testng-1.0.dtd" >
<suite name="My test suite">
<test name="testing">
<parameter name="number" value="2"/>
<classes>
<class name="TestNGTest6" />
</classes>
</test>
</suite>
Result: Parameterized Number is : 2
TestNG
▪ Testing example. Converting the character to ASCII or vice
verse.
package com.softserve.edu;
public class CharUtils
{
public static int CharToASCII(final char character){
return (int)character;
}
public static char ASCIIToChar(final int ascii){
return (char)ascii;
}
}
TestNG
import org.testng.Assert;
import org.testng.annotations.*;
public class CharUtilsTest {
@DataProvider
public Object[][] ValidDataProvider() {
return new Object[][]{
{ 'A', 65 }, { 'a', 97 },
{ 'B', 66 }, { 'b', 98 },
{ 'C', 67 }, { 'c', 99 },
{ 'D', 68 }, { 'd', 100 },
{ 'Z', 90 }, { 'z', 122 },
{ '1', 49 }, { '9', 57 }, };
}
TestNG
@Test(dataProvider = "ValidDataProvider")
public void CharToASCIITest(final char character,
final int ascii) {
int result = CharUtils.CharToASCII(character);
Assert.assertEquals(result, ascii); }
@Test(dataProvider = "ValidDataProvider")
public void ASCIIToCharTest(final char character,
final int ascii) {
char result = CharUtils.ASCIIToChar(ascii);
Assert.assertEquals(result, character);
} }
TestNG
▪ Dependency Test. TestNG uses “dependOnMethods“ to
implement the dependency testing. If the dependent method
fails, all the subsequent test methods will be skipped, not
marked as failed.
▪ The “method2()” will execute only if “method1()” is run
successfully, else “method2()” will skip
import org.testng.annotations.*;
public class TestNGTest10 {
@Test
public void method1() {
System.out.println("This is method 1"); }
@Test(dependsOnMethods={"method1"})
public void method2() {
System.out.println("This is method 2"); } }
TestNG
public class ConcurrencyTest2 extends Assert {
// some staff here
@DataProvider(parallel = true)
public Object[][] concurrencyData() {
return new Object[][] {
{"1", "2"}, {"3", "4"},
{"5", "6"}, {"7", "8"},
{"9", "10"}, {"11", "12"},
{"13", "14"}, {"15", "16"},
{"17", "18"}, {"19", "20"},
};
}
TestNG
▪ Set parallel option at the date of the provider in true.
▪ Tests for each data set to be launched in a separate thread.
@Test(dataProvider = "concurrencyData")
public void testParallelData(String first, String second) {
final Thread thread = Thread.currentThread();
System.out.printf("#%d %s: %s : %s", thread.getId(),
thread.getName(), first, second);
System.out.println();
}
}
Junit and testNG

More Related Content

PPT
PDF
TestNG vs. JUnit4
PPTX
Test NG Framework Complete Walk Through
PPTX
Junit4&testng presentation
PDF
TestNG introduction
PPTX
TestNG Framework
PPTX
TestNG vs JUnit: cease fire or the end of the war
TestNG vs. JUnit4
Test NG Framework Complete Walk Through
Junit4&testng presentation
TestNG introduction
TestNG Framework
TestNG vs JUnit: cease fire or the end of the war

What's hot (20)

PPTX
Test ng tutorial
PDF
TestNg_Overview_Config
PDF
Test ng for testers
PDF
TestNG - The Next Generation of Unit Testing
PDF
PPTX
Introduction of TestNG framework and its benefits over Junit framework
PPTX
TestNG Session presented in PB
PPTX
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
ODP
Test ng
PPTX
TestNG with selenium
PPTX
PPT
Simple Unit Testing With Netbeans 6.1
PPSX
PPS
JUnit Presentation
PPTX
PDF
Unit testing with JUnit
PPTX
Introduction to JUnit
PPTX
TestNG vs Junit
PPTX
Unit Testing with JUnit4 by Ravikiran Janardhana
PPTX
Java Unit Testing
Test ng tutorial
TestNg_Overview_Config
Test ng for testers
TestNG - The Next Generation of Unit Testing
Introduction of TestNG framework and its benefits over Junit framework
TestNG Session presented in PB
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
Test ng
TestNG with selenium
Simple Unit Testing With Netbeans 6.1
JUnit Presentation
Unit testing with JUnit
Introduction to JUnit
TestNG vs Junit
Unit Testing with JUnit4 by Ravikiran Janardhana
Java Unit Testing
Ad

Viewers also liked (16)

PPTX
JUnit- A Unit Testing Framework
PPTX
Maven TestNg frame work (1) (1)
PPTX
Me and my goals
PPS
Berntsen -chicago_my_home_town_-_08-10-11
PDF
Ad tracker
PDF
TOPTEN Mediakit For New Home Builders
PDF
Green Fax Agm
PPTX
Educ1751 Assignment 1
PPTX
pay it foward
PDF
Ad Tracker Report
PDF
Green fax agm
PPT
05 junit
PPT
PPTX
人工知能の研究開発方法について20160927
JUnit- A Unit Testing Framework
Maven TestNg frame work (1) (1)
Me and my goals
Berntsen -chicago_my_home_town_-_08-10-11
Ad tracker
TOPTEN Mediakit For New Home Builders
Green Fax Agm
Educ1751 Assignment 1
pay it foward
Ad Tracker Report
Green fax agm
05 junit
人工知能の研究開発方法について20160927
Ad

Similar to Junit and testNG (20)

PPTX
Unit Testing in Java
DOCX
Junit With Eclipse
PDF
junit-160729073220 eclipse software testing.pdf
PDF
IT Talk TestNG 6 vs JUnit 4
PDF
Unit testing and scaffolding
PPTX
unit 1 (1).pptx
PDF
Unit testing with JUnit
PPTX
Testing with Junit4
ODP
Embrace Unit Testing
PDF
L08 Unit Testing
PDF
Unit Testing on Android - Droidcon Berlin 2015
PPT
3 j unit
PDF
Junit Recipes - Intro
PDF
Software Testing - Invited Lecture at UNSW Sydney
PDF
How and what to unit test
PPS
J unit presentation
PDF
The Art of Unit Testing Feedback
PPT
Assessing Unit Test Quality
Unit Testing in Java
Junit With Eclipse
junit-160729073220 eclipse software testing.pdf
IT Talk TestNG 6 vs JUnit 4
Unit testing and scaffolding
unit 1 (1).pptx
Unit testing with JUnit
Testing with Junit4
Embrace Unit Testing
L08 Unit Testing
Unit Testing on Android - Droidcon Berlin 2015
3 j unit
Junit Recipes - Intro
Software Testing - Invited Lecture at UNSW Sydney
How and what to unit test
J unit presentation
The Art of Unit Testing Feedback
Assessing Unit Test Quality

Recently uploaded (20)

PPTX
ai tools demonstartion for schools and inter college
PDF
Digital Strategies for Manufacturing Companies
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Introduction to Artificial Intelligence
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
history of c programming in notes for students .pptx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Nekopoi APK 2025 free lastest update
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Transform Your Business with a Software ERP System
ai tools demonstartion for schools and inter college
Digital Strategies for Manufacturing Companies
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Softaken Excel to vCard Converter Software.pdf
Operating system designcfffgfgggggggvggggggggg
Introduction to Artificial Intelligence
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Navsoft: AI-Powered Business Solutions & Custom Software Development
How to Choose the Right IT Partner for Your Business in Malaysia
history of c programming in notes for students .pptx
Odoo POS Development Services by CandidRoot Solutions
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Nekopoi APK 2025 free lastest update
Wondershare Filmora 15 Crack With Activation Key [2025
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PTS Company Brochure 2025 (1).pdf.......
Transform Your Business with a Software ERP System

Junit and testNG

  • 1. Java. Unit and Integration Testing IT Academy 22/07/2014
  • 2. Agenda ▪Unit and Integration Testing ▪JUnit ▪TestNG ▪Parallel Tests Running ▪Case studies
  • 3. Testing Types. Unit Testing ▪ Unit testing is a procedure used to validate that individual units of source code are working properly. ▪ The goal of unit testing is to isolate each part of the program and show that the individual parts are correct.
  • 4. Integration Testing ▪ Integration testing is the phase of software testing in which individual software modules are combined and tested as a group. ▪ This is testing two or more modules or functions together with the intent of finding interface defects between the modules or functions.
  • 5. Integration Testing ▪ Task: Database scripts, application main code and GUI components were developed by different programmers. There is need to test the possibility to use these 3 parts as one system. ▪ Integration Testing Procedure: Combine 3 parts into one system and verify interfaces (check interaction with database and GUI). ▪ Defect: “Materials” button action returns not list of books but list of available courses.
  • 6. Unit / Integration Testing public class One { private String text; public One() { // Code } // Functionality public int calc() { int result = 0; // Code return result; } // get, set, etc. }
  • 7. Unit / Integration Testing public class Two { private One one; public Two(One one) { this.one = one; } public String resume() { // Functionality return one.getText() + "_" + one.calc().toString(); } // Code }
  • 8. Unit / Integration Testing public class Appl { public static void main(String[] args) { One one = new One(); one.setText("data“); Two two = new Two(one); two.resume(); // etc } }
  • 9. Unit / Integration Testing public class TwoTest { @Test public void TestResume() { One one = new One(); one.setText("Integration Test“); Two two = new Two(one); // TODO Initialize to an appropriate value String expected = "Integration Test_..."; // Result ??? String actual; actual = two.resume(); Assert.AreEqual(expected, actual); } }
  • 10. Unit / Integration Testing ▪ A method stub or simply stub in software development is a piece of code used to stand in for some other programming functionality. ▪ A stub may simulate the behavior of existing code or be a temporary substitute for yet-to-be-developed code. public interface IOne { void setText(String text); String getText(); int calc(); }
  • 11. Unit / Integration Testing public class One implements IOne { private String text; public One() { // Code } // Functionality public int calc() { int result = 0; // Code return result; } // get, set, etc. }
  • 12. Unit / Integration Testing public class Two { private IOne one; public Two(IOne one) { this.one = one; } public String resume() { // Functionality return one.getText() + "_" + one.calc().toString(); } // Code }
  • 13. Unit / Integration Testing ▪ Stub – a piece of code used to stand in for some other programming functionality. public class StubOne extends IOne { private String text; public StubOne() { } public void setText(String text) { } public String getText() { return “” } public int Calc() { return 0; } }
  • 14. Unit / Integration Testing public class TwoTest { @Test public void TestResume() { IOne one = new StubOne(); one.setText(" Unit Test“); Two two = new Two(one); // TODO Initialize to an appropriate value String expected = " Unit Test_..."; // Result !!! String actual; actual = two.resume(); Assert.AreEqual(expected, actual); } }
  • 15. Junit 3 import junit.framework.TestCase; рublic class AddJavaTest extends TestCase { protected void setUp() throws Exception { // create object } protected void tearDown() throws Exception { // to free resources } public AddJavaTest(String name){ super (name); } public void testSimpleAddition( ){ assertTrue(expect == actual); } } ▪deprecated
  • 16. JUnit Assert class contains many different overloaded methods. http://www.junit.org/ https ://github.com/kentbeck/junit/wiki assertEquals(long expected, long actual) Asserts that two longs are equal. assertTrue(boolean condition) Asserts that a condition is true. assertFalse(boolean condition) Asserts that a condition is false. assertNotNull(java.lang.Object object) Asserts that an object isn't null. assertNull(java.lang.Object object) Asserts that an object is null.
  • 17. JUnit assertSame(java.lang.Object expected, java.lang.Object actual) Asserts that two objects refer to the same object. assertNotSame(java.lang.Object unexpected, java.lang.Object actual) Asserts that two objects do not refer to the same object. assertArrayEquals(byte[] expecteds, byte[] actuals) Asserts that two byte arrays are equal. fail() Fails a test with no message. fail(java.lang.String message) Fails a test with the given message.
  • 18. Junit 4 import org.junit.Test; import org.junit.Assert; public class MathTest { @Test public void testEquals( ) { Assert.assertEquals(4, 2 + 2); Assert.assertTrue(4 == 2 + 2); } @Test public void testNotEquals( ) { Assert.assertFalse(5 == 2 + 2); } }
  • 19. JUnit
  • 20. JUnit
  • 21. JUnit
  • 22. JUnit
  • 23. TestNG ▪ JUnit 4 and TestNG are both very popular unit test framework in Java. ▪ TestNG (Next Generation) is a testing framework which inspired by JUnit and NUnit, but introducing many new innovative functionality like dependency testing, grouping concept to make testing more powerful and easier to do. It is designed to cover all categories of tests: unit, functional, end- to-end, integration, etc.
  • 24. TestNG ▪ Setting up TestNG with Eclipse. ▪ Click Help –> Install New Software ▪ Type http://beust.com/eclipse in the "Work with" edit box and click ‘Add’ button. ▪ Click Next and click on the radio button "I accept the terms of the license agreement“. ▪ After the installation, it will ask for restart of Eclipse. Then restart the Eclipse. ▪ Once the Eclipse is restarted, we can see the TestNG icons & menu items as in the below figures.
  • 25. TestNG ▪ To create a new TestNG class, select the menu File/New/TestNG
  • 26. TestNG ▪ The next page lets you specify where that file will be created
  • 27. TestNG ▪ Basic usage import java.util.*; import org.testng.Assert; import org.testng.annotations.*; public class TestNGTest1 { private Collection collection; @BeforeClass public void oneTimeSetUp() { System.out.println("@BeforeClass - oneTimeSetUp"); } @AfterClass public void oneTimeTearDown() { // one-time cleanup code System.out.println("@AfterClass - oneTimeTearDown"); }
  • 28. TestNG @BeforeMethod public void setUp() { collection = new ArrayList(); System.out.println("@BeforeMethod - setUp"); } @AfterMethod public void tearDown() { collection.clear(); System.out.println("@AfterMethod - tearDown"); }
  • 29. TestNG @Test public void testEmptyCollection() { Assert.assertEquals(collection.isEmpty(),true); System.out.println("@Test - testEmptyCollection"); } @Test public void testOneItemCollection() { collection.add("itemA"); Assert.assertEquals(collection.size(),1); System.out.println("@Test - testOneItemCollection"); } }
  • 30. TestNG ▪ Expected Exception Test ▪ The divisionWithException() method will throw an ArithmeticException Exception, since this is an expected exception, so the unit test will pass. import org.testng.annotations.*; public class TestNGTest2 { @Test(expectedExceptions = ArithmeticException.class) public void divisionWithException() { int i = 1/0; } }
  • 31. TestNG ▪ Suite Test. The “Suite Test” means bundle a few unit test cases and run it together. ▪ In TestNG, XML file is use to define the suite test. <!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > <suite name="My test suite"> <test name="testing"> <classes> <class name="TestNGTest1" /> <class name="TestNGTest2" /> </classes> </test> </suite> ▪"TestNGTest1" and "TestNGTest2" will execute together
  • 32. TestNG ▪ Parameterized Test ▪ In TestNG, XML file or “@DataProvider” is used to provide vary parameter for unit testing. ▪ Declare “@Parameters” annotation in method which needs parameter testing, the parametric data will be provide by TestNG’s XML configuration files. By doing this, you can reuse a single test case with different data sets easily. import org.testng.annotations.*; public class TestNGTest6 { @Test @Parameters(value="number") public void parameterIntTest(int number) { System.out.println("Parameterized Number is: " + number); } }
  • 33. TestNG <!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > <suite name="My test suite"> <test name="testing"> <parameter name="number" value="2"/> <classes> <class name="TestNGTest6" /> </classes> </test> </suite> Result: Parameterized Number is : 2
  • 34. TestNG ▪ Testing example. Converting the character to ASCII or vice verse. package com.softserve.edu; public class CharUtils { public static int CharToASCII(final char character){ return (int)character; } public static char ASCIIToChar(final int ascii){ return (char)ascii; } }
  • 35. TestNG import org.testng.Assert; import org.testng.annotations.*; public class CharUtilsTest { @DataProvider public Object[][] ValidDataProvider() { return new Object[][]{ { 'A', 65 }, { 'a', 97 }, { 'B', 66 }, { 'b', 98 }, { 'C', 67 }, { 'c', 99 }, { 'D', 68 }, { 'd', 100 }, { 'Z', 90 }, { 'z', 122 }, { '1', 49 }, { '9', 57 }, }; }
  • 36. TestNG @Test(dataProvider = "ValidDataProvider") public void CharToASCIITest(final char character, final int ascii) { int result = CharUtils.CharToASCII(character); Assert.assertEquals(result, ascii); } @Test(dataProvider = "ValidDataProvider") public void ASCIIToCharTest(final char character, final int ascii) { char result = CharUtils.ASCIIToChar(ascii); Assert.assertEquals(result, character); } }
  • 37. TestNG ▪ Dependency Test. TestNG uses “dependOnMethods“ to implement the dependency testing. If the dependent method fails, all the subsequent test methods will be skipped, not marked as failed. ▪ The “method2()” will execute only if “method1()” is run successfully, else “method2()” will skip import org.testng.annotations.*; public class TestNGTest10 { @Test public void method1() { System.out.println("This is method 1"); } @Test(dependsOnMethods={"method1"}) public void method2() { System.out.println("This is method 2"); } }
  • 38. TestNG public class ConcurrencyTest2 extends Assert { // some staff here @DataProvider(parallel = true) public Object[][] concurrencyData() { return new Object[][] { {"1", "2"}, {"3", "4"}, {"5", "6"}, {"7", "8"}, {"9", "10"}, {"11", "12"}, {"13", "14"}, {"15", "16"}, {"17", "18"}, {"19", "20"}, }; }
  • 39. TestNG ▪ Set parallel option at the date of the provider in true. ▪ Tests for each data set to be launched in a separate thread. @Test(dataProvider = "concurrencyData") public void testParallelData(String first, String second) { final Thread thread = Thread.currentThread(); System.out.printf("#%d %s: %s : %s", thread.getId(), thread.getName(), first, second); System.out.println(); } }