Make Selenium Easy

TestNG Tutorials 53: DataProvider in TestNG – Is It Mandatory To Have Return Type as Object in DataProvider Method

Hello Guys,

DataProvider method in TestNG is a way to provide test data to Test annotated methods in a TestNG class. A typical DataProvider method looks like as below:

You can see that return type of above DataProvider method is an Object array. Is it mandatory to have return type as Object only? Answer is NO. But I see some people understands that it should be Object always which is not correct.

In fact you can have return type of a DataProvider method other than Object as well. If your all data is of type String, use String. See an example below:

Output:

[RemoteTestNG] detected TestNG version 6.14.2
Registered student with details: Mukesh Otwani Motwani@gmail.com
Registered student with details: Amod Mahajan Amahajan@hotmail.com
Registered student with details: Animesh Prashant aprashant@gmail.com
Registered student with details: Ankur Singh asingh@gmail.com
Registered student with details: Amritansh Kumar akumar@gmail.com
PASSED: studentRegistration("Mukesh", "Otwani", "Motwani@gmail.com")
PASSED: studentRegistration("Amod", "Mahajan", "Amahajan@hotmail.com")
PASSED: studentRegistration("Animesh", "Prashant", "aprashant@gmail.com")
PASSED: studentRegistration("Ankur", "Singh", "asingh@gmail.com")
PASSED: studentRegistration("Amritansh", "Kumar", "akumar@gmail.com")

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


===============================================
Default suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================

Note: You can not have return type as primitive e.g. int etc. If you use it, you will get class cast exception. Refer example below:

Output:

[RemoteTestNG] detected TestNG version 6.14.2
FAILED: studentRegistration
java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;

Solution is that you need to use wrapper class of primitive. E.g. Integer for int.

[RemoteTestNG] detected TestNG version 6.14.2
n1 =1 n2 =2 n3 =3
n1 =3 n2 =4 n3 =5
PASSED: studentRegistration(1, 2, 3)
PASSED: studentRegistration(3, 4, 5)

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


===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

So it’s clear now that a DataProvider method can return other types as well not only Object. Return type as Object is preferred when we need to return heterogenous data. E.g Integer, String etc all from one method. Since Object class is super class of all other classes in Java so Object class can provide reference to subclass objects by following Upcasting concept of Java.

Hope you are clear with above concept.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.