TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

TestNG

TestNG DataProvider Basic Example.

What is DataProvider ?

In this post we will look into DataProvider Basic Example. @DataProvider allows a @Test method which uses the data provider to be executed multiple times. By using DataProvider the @Test method would be executed using the same instance of the test class to which the test method belongs.

First things first , we will see a code example of how to execute the test method multiple times.

package com.testingdocs.testng.sample;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderExample 
{
 @DataProvider(name="org")
 
 public static Object[][] employeeData(){ 
 Object[][] object = new Object[2][3]; 
 
 object[0][0]="Manager"; 
 object[0][1]="Regan"; 
 object[0][2]=12000; 
 
 object[1][0]="Employee"; 
 object[1][1]="John"; 
 object[1][2]=8000; 
 
 
 return object; 
 } 
 
 
 @Test(dataProvider="org") 
 public void runforEachEmployee(String role, String empname, int salary)
 { 
 System.out.println("******Emp Record***********");
 System.out.println(empname); 
 System.out.println(role); 
 System.out.println(salary); 
 } 

}

The basic idea of the example is to run the test method for each employee.
Lets us run the example and see what happens.The test method would run for each
employee.
In this example, both the test method and data provider reside in the same class.

Program output :

******Emp Record***********
Regan
Manager
12000
******Emp Record***********
John
Employee
8000

PASSED: runforEachEmployee(“Manager”, “Regan”, 12000)
PASSED: runforEachEmployee(“Employee”, “John”, 8000)

===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================

One of the common errors or problems working with data provider happens when the data type mismatch with data provider and the test method. In the next section we will see the scenario of what happens when there is a data type mismatch .

We will inject an error by specifying the salary with a string instead of a number.

As shown below we would get a Method match exception as the method expects an integer.

——————————————————

org.testng.internal.reflect.MethodMatcherException:
Data provider mismatch
Method: runforEachEmployee([Parameter{index=0, type=java.lang.String, 
declaredAnnotations=[]}, 
Parameter{index=1, type=java.lang.String, declaredAnnotations=[]}, 
Parameter{index=2, type=int,
 declaredAnnotations=[]}])
Arguments: [(java.lang.String)Manager,(java.lang.String)Regan,
(java.lang.String)testingDocs]
 at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments
(DataProviderMethodMatcher.java:52)

 

Internally, TestNG API checks for the conforming arguments of the DataProvider and the test method as shown:

 

@Override
 public Object[] getConformingArguments() throws MethodMatcherException {
 if (ThreeStateBoolean.NONE.equals(getConforms())) {
 conforms();
 }
 if (matchingMatcher != null) {
 return matchingMatcher.getConformingArguments();
 }
 throw new MethodMatcherException("Data provider mismatch", 
getContext().getMethod(), getContext().getArguments());
 }

 

 

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

Related Posts

Tests TestNG Suite testng.xml

TestNG /

Run tests from TestNG Suite testng.xml file

Add TestNG library

TestNG /

Add the TestNG library to the project

new TestNG class

TestNG /

Create TestNG test class in Eclipse IDE.

TestNG Plugin IntelliJ

TestNG /

Enable TestNG in IntelliJ IDE

Installing Eclipse from Update Site

TestNG /

Install TestNG latest version from the update site

‹ TestNG Factory Annotation› TestNG Framework Questions

Recent Posts

  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • RAPTOR Editions
  • Flowgorithm Conditional Breakpoint Statement
  • Flowgorithm Read Numbers from File Example
  • Search Text File Flowchart Example
  • Flowgorithm Turtle Graphics Symbols
  • Draw Circle using Flowgorithm Turtle
  • Draw Parallel Lines using Flowgorithm Graphics

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version