Json 文件作为 TestNG 中的数据提供者
Json file as data provider in TestNG
下面是我的JSON结构:
我想在 Java 中编写一个方法,该方法应仅将 SuperParent 作为输入参数,将 return "Registration Data" 和 "Registration Data1" 及其子元素作为 TestNG 数据提供者对象。
"SuperParent":
{
"Registration Data" :
{
"FirstName" : "Aditya Kumar",
"LastName" : "Roy",
"Phone" : "7589698874",
"Email" : "aditya@test.com"
},
"Registration Data1" :
{
"FirstName" : "Preety",
"LastName" : "Kakkar",
"Phone" : "2225556669",
"Email" : "preety@test.com"
}
}
我写了下面的代码,但它抛出了 Dataprovider 不匹配异常
public static Object[][] getTest(String regionName)
{
JsonParser jsonParser = new JsonParser();
JsonObject jsonObj = jsonParser.parse(new FileReader(JSONpath)).getAsJsonObject();
JsonObject region =jsonObj.getAsJsonObject(regionName);
Object[][] returnData = new Object[100][region.size()];
int j = 0;
for (Map.Entry<String, JsonElement> entry : region.getAsJsonObject().entrySet())
{
returnData[0][j] = (region.toString());
j++;
}
return returnData;
}
FAILED: userRegistration
org.testng.internal.reflect.MethodMatcherException:
Data provider mismatch
Method: userRegistration([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) {FirstName:Aditya Kumar,LastName:Roy,Phone:7589698874,Email:aditya@test.com},(java.lang.String) {FirstName:Preety,LastName:Kakkar,Phone:2225556669,Email:preety@test.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:983)
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)
我记得我之前尝试将 object[][] 数组数据转换为 String 时遇到了同样的问题。如果您将在第二行看到,我使用 Object 作为参数类型。我已经测试了下面的代码并且工作正常。也许您可以对其进行一些修改并在您的项目中使用它。
@Test( dataProvider = "data" )
public void test(Object key, Object data)
{
System.out.println( "Key: " + key);
System.out.println( "Data: " + data );
}
@DataProvider( name = "data" )
public Object[][] getData()
{
JSONParser parser = new org.json.simple.parser.JSONParser();
Map<String, JSONObject> dataMap = new HashMap<String, JSONObject>();
Iterator entriesIterator = null;
try
{
JSONObject contentObj = (JSONObject)parser.parse(new FileReader(new File(System.getProperty("user.dir") + "/src/test/resources/json/data.json")));
JSONObject dataObj = (JSONObject)contentObj.get("SuperParent");
dataObj.keySet().forEach(key -> {
dataMap.put(String.valueOf(key), (JSONObject) dataObj.get(key));
});
Set entries = dataObj.entrySet();
entriesIterator = entries.iterator();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Object[][] arr = new Object[dataMap.size()][2];
int i = 0;
while(entriesIterator.hasNext()){
Map.Entry mapping = (Map.Entry) entriesIterator.next();
arr[i][0] = mapping.getKey();
arr[i][1] = mapping.getValue();
i++;
}
return arr;
}
下面是我的JSON结构: 我想在 Java 中编写一个方法,该方法应仅将 SuperParent 作为输入参数,将 return "Registration Data" 和 "Registration Data1" 及其子元素作为 TestNG 数据提供者对象。
"SuperParent":
{
"Registration Data" :
{
"FirstName" : "Aditya Kumar",
"LastName" : "Roy",
"Phone" : "7589698874",
"Email" : "aditya@test.com"
},
"Registration Data1" :
{
"FirstName" : "Preety",
"LastName" : "Kakkar",
"Phone" : "2225556669",
"Email" : "preety@test.com"
}
}
我写了下面的代码,但它抛出了 Dataprovider 不匹配异常
public static Object[][] getTest(String regionName)
{
JsonParser jsonParser = new JsonParser();
JsonObject jsonObj = jsonParser.parse(new FileReader(JSONpath)).getAsJsonObject();
JsonObject region =jsonObj.getAsJsonObject(regionName);
Object[][] returnData = new Object[100][region.size()];
int j = 0;
for (Map.Entry<String, JsonElement> entry : region.getAsJsonObject().entrySet())
{
returnData[0][j] = (region.toString());
j++;
}
return returnData;
}
FAILED: userRegistration
org.testng.internal.reflect.MethodMatcherException:
Data provider mismatch
Method: userRegistration([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) {FirstName:Aditya Kumar,LastName:Roy,Phone:7589698874,Email:aditya@test.com},(java.lang.String) {FirstName:Preety,LastName:Kakkar,Phone:2225556669,Email:preety@test.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:983)
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)
我记得我之前尝试将 object[][] 数组数据转换为 String 时遇到了同样的问题。如果您将在第二行看到,我使用 Object 作为参数类型。我已经测试了下面的代码并且工作正常。也许您可以对其进行一些修改并在您的项目中使用它。
@Test( dataProvider = "data" )
public void test(Object key, Object data)
{
System.out.println( "Key: " + key);
System.out.println( "Data: " + data );
}
@DataProvider( name = "data" )
public Object[][] getData()
{
JSONParser parser = new org.json.simple.parser.JSONParser();
Map<String, JSONObject> dataMap = new HashMap<String, JSONObject>();
Iterator entriesIterator = null;
try
{
JSONObject contentObj = (JSONObject)parser.parse(new FileReader(new File(System.getProperty("user.dir") + "/src/test/resources/json/data.json")));
JSONObject dataObj = (JSONObject)contentObj.get("SuperParent");
dataObj.keySet().forEach(key -> {
dataMap.put(String.valueOf(key), (JSONObject) dataObj.get(key));
});
Set entries = dataObj.entrySet();
entriesIterator = entries.iterator();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Object[][] arr = new Object[dataMap.size()][2];
int i = 0;
while(entriesIterator.hasNext()){
Map.Entry mapping = (Map.Entry) entriesIterator.next();
arr[i][0] = mapping.getKey();
arr[i][1] = mapping.getValue();
i++;
}
return arr;
}