TestNG Tutorials 65 – DataProvider in TestNG – All Possible Causes of Occurring Data Provider Mismatch Exception

Hello Folks,

DataProvider is an awesome functionality provided by TestNG to achieve Data Driven Testing. You must have already seen many posts on DataProvider on my blog. If not, I will advise you to go through them.

If you have used DataProvider in your tests, you must have encounter “Data provider mismatch” exception. I see many beginners and experienced professionals struggle at this point as they are not aware of the basic reasons of causing this. So, I would like to explain all possible reasons of occuring this exception here so that it will be helpful to solve when you encounter.

Cause 1: When we do not accept arguments in Test method while using DataProvider with Test Method:-

Base Rule – If you are using a DataProvider with your test, you must need to accept the arguments. It is not OPTIONAL. You must have to accept arguments in your Test Method.

Java Code:

 

Output:

[RemoteTestNG] detected TestNG version 6.14.2
FAILED: mismatchDataProvider1
org.testng.internal.reflect.MethodMatcherException: 
Data provider mismatch
Method: mismatchDataProvider1([])
Arguments: [(java.lang.String) Mukesh,(java.lang.String) Otwani,(java.lang.String) Motwani@gmail.com]
	at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45)
	at org.testng.internal.Parameters.injectParameters(Parameters.java:796)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:982)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.privateRun(TestRunner.java:648)
	at org.testng.TestRunner.run(TestRunner.java:505)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
	at org.testng.SuiteRunner.run(SuiteRunner.java:364)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
	at org.testng.TestNG.runSuites(TestNG.java:1049)
	at org.testng.TestNG.run(TestNG.java:1017)
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


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


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


Cause 2: When we accept less arguments in Test method while using DataProvider with Test Method:-

Base Rule- You must need to accept same number of arguments in your test method as provided by DataProvider method. If your test method returns N arguments in a set, your test method must accept all i.e. N arguments. No More No Less.

Java Code:

Output:

[RemoteTestNG] detected TestNG version 6.14.2
FAILED: mismatchDataProvider1
org.testng.internal.reflect.MethodMatcherException: 
Data provider mismatch
Method: mismatchDataProvider1([Parameter{index=0, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String, declaredAnnotations=[]}])
Arguments: [(java.lang.String) Mukesh,(java.lang.String) Otwani,(java.lang.String) Motwani@gmail.com]
	at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45)
	at org.testng.internal.Parameters.injectParameters(Parameters.java:796)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:982)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.privateRun(TestRunner.java:648)
	at org.testng.TestRunner.run(TestRunner.java:505)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
	at org.testng.SuiteRunner.run(SuiteRunner.java:364)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
	at org.testng.TestNG.runSuites(TestNG.java:1049)
	at org.testng.TestNG.run(TestNG.java:1017)
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


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


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


Cause 3: When we accept more arguments in Test method while using DataProvider with Test Method:-

Base Rule- You must need to accept same number of arguments in your test method as provided by DataProvider method. If your test method returns N arguments in a set, your test method must accept all i.e. N arguments. No More No Less.

Java Code:

Output:

[RemoteTestNG] detected TestNG version 6.14.2
FAILED: mismatchDataProvider1
org.testng.internal.reflect.MethodMatcherException: 
Data provider mismatch
Method: mismatchDataProvider1([Parameter{index=0, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=2, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=3, type=java.lang.String, declaredAnnotations=[]}])
Arguments: [(java.lang.String) Mukesh,(java.lang.String) Otwani,(java.lang.String) Motwani@gmail.com]
	at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45)
	at org.testng.internal.Parameters.injectParameters(Parameters.java:796)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:982)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.privateRun(TestRunner.java:648)
	at org.testng.TestRunner.run(TestRunner.java:505)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
	at org.testng.SuiteRunner.run(SuiteRunner.java:364)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
	at org.testng.TestNG.runSuites(TestNG.java:1049)
	at org.testng.TestNG.run(TestNG.java:1017)
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


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


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


Cause 4: When we try to downcast the type of arguments provided by DataProvider in our Test method-

Base Rule – Suppose if your DataProvider method is returning an argument of type String, you can accept it as String or its Super class ( E.g. Object). If you try to accept String into Integer etc, You will face Data Provider mismatch exception.

Java Code:

Output:

[RemoteTestNG] detected TestNG version 6.14.2
FAILED: mismatchDataProvider1
org.testng.internal.reflect.MethodMatcherException: 
Data provider mismatch
Method: mismatchDataProvider1([Parameter{index=0, type=java.lang.Integer, declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=2, type=java.lang.String, declaredAnnotations=[]}])
Arguments: [(java.lang.String) Mukesh,(java.lang.String) Otwani,(java.lang.String) Motwani@gmail.com]
	at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45)
	at org.testng.internal.Parameters.injectParameters(Parameters.java:796)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:982)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.privateRun(TestRunner.java:648)
	at org.testng.TestRunner.run(TestRunner.java:505)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
	at org.testng.SuiteRunner.run(SuiteRunner.java:364)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
	at org.testng.TestNG.runSuites(TestNG.java:1049)
	at org.testng.TestNG.run(TestNG.java:1017)
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


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


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

Cause 5: When we use both Parameters and DataProvider in our Test method-

Base Rule- We can not use DataProvider and Parameters together in a Test method.

Java Code:

We need to use testng.xml for this.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
  <parameter name="age" value="21" />
		<classes>
			<class name="DataProvider.DataProviderMismatchCause1">
			</class>
		</classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

When you run it, you will get Data Provider mismatch exception.

Hope, you will keep above points in mind while using DataProvider.

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

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

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.
%d bloggers like this: