I tried to implement builder pattern into my code and I'm stuck with DataProvider. When I'm tring launch test I get an error:
org.testng.internal.reflect.MethodMatcherException:
[public void api.tests.DpTests.msgCenterCase(testdata.dataprovider.Dataprovider2)] has no parameters defined but was found to be using a data provider (either explicitly specified or inherited from class level annotation).
Data provider mismatch
Method: msgCenterCase([testdata.dataprovider.Dataprovider2 arg0])
Arguments: [(testdata.Dataprovider2TestData$Dataprovider2TestDataBuilder) Dataprovider2TestData.Dataprovider2TestDataBuilder(createHeader=String, updateHeader=String, createDP=DataProviderObject(field1=field, field2=field, field3=field, field4=field), updateDP=null)]
at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:46)
at org.testng.internal.Parameters.injectParameters(Parameters.java:909)
at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:37)
at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:969)
at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:194)
at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.testng.TestRunner.privateRun(TestRunner.java:829)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:437)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:431)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:391)
at org.testng.SuiteRunner.run(SuiteRunner.java:330)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1256)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1176)
at org.testng.TestNG.runSuites(TestNG.java:1099)
at org.testng.TestNG.run(TestNG.java:1067)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
Test ignored.
I can't see what's the problem because in builder class I am building whole object and pass it to method as argument.
Test class:
package api.tests;
import client.ApiRequestModel;
import org.testng.annotations.Test;
import testdata.Dataprovider2TestData;
import testdata.dataprovider.Dataprovider2;
public class DpTests {
private Dataprovider2TestData createDP;
@Test(dataProvider = "dataproviderTest", dataProviderClass = Dataprovider2.class)
public void msgCenterCase(Dataprovider2 dataprovider) {
ApiRequestModel DPRequest = ApiRequestModel.builder()
.endpoint("/DPEndpoint")
.body(createDP.getCreateDP())
.build();
}
}
DataProvider class
package testdata.dataprovider;
import model.fulfillment.DataProviderObject;
import org.testng.annotations.DataProvider;
import testdata.Dataprovider2TestData;
public class Dataprovider2 {
@DataProvider(name = "dataproviderTest")
public static Object[][] headerVersions() {
return new Object[][]{
{
Dataprovider2TestData.builder()
.createHeader("String")
.updateHeader("String")
.createDP(
DataProviderObject.builder()
.field1("field")
.field2("field")
.field3("field")
.field4("field")
.build())
}
};
}
};
Data provider Test Data class
package testdata;
import lombok.Builder;
import lombok.Data;
import model.fulfillment.DataProviderObject;
@Data
@Builder
public class Dataprovider2TestData {
private String createHeader;
private String updateHeader;
private DataProviderObject createDP;
private DataProviderObject updateDP;
}
Data Provider object class
package model.dp
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class DataProviderObject {
private String field1;
private String field2;
private String field3;
private String field4;
}
I was trying make data provider as non-static and some combination with data. Everything works fine when I use simple Strings in DataProvider but with object it doesn't.
Your data provider method dataproviderTest is returning an array of arrays, where each inner array contains a single object of type Dataprovider2TestData. But, the test method msgCenterCase expects a single argument of type Dataprovider2.
You need to change the data provider method to return an array of Dataprovider2 objects. Modify the data provider method as below :
@DataProvider(name = "dataproviderTest")
public static Object[][] headerVersions() {
return new Object[][]{
{
Dataprovider2.builder()
.createHeader("String")
.updateHeader("String")
.createDP(
DataProviderObject.builder()
.field1("field")
.field2("field")
.field3("field")
.field4("field")
.build())
.build()
}
};
}