-
Notifications
You must be signed in to change notification settings - Fork 19
Passing JSON Object in test data
Apart from passing string literals that are converted at runtime by EasyTest into appropriate objects using Editors/Converters or standard Java object conversion support, the users of EasyTest can also pass JSON objects in their test data files. These JSON objects are then converted into the right instance by the EasyTest framework and passed as input parameter to the test method.
Lets look at an example of the same.
Like always, first define a test class that loads data from an XLS file :
@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths="jsonBasedData.xls")
public class TestJSON {
@Test
public void testJSONObject(@Param(name="Item")Item item ){
....
}
Next lets look at how to define the data in the Excel file:
|getJSONObject | Item
| {"itemId":{"id":23},"itemType":"BOOK","description":"batMAN"}
|{"itemId":{"id":24},"itemType":"JOURNAL","description":"SPIDERMAN"}
|{"itemId":{"id":25},"itemType":"E-BOOK","description":"SUPERMAN"}
where the | (Pipe Symbol) demarcates the boundary of a cell in an Excel file.
Thats it. When you run your test, EasyTest framework will convert a JSON string into an object of Type "Item" and pass it to the method testJSONObject as test input parameter. Note that JSON expects that the class to which the String data will be mapped should have a no-arg constructor.
Since CSV is a comma separated file where comma and double quotes have special meanings, it was not possible to use comma and double quotes as we could use in Excel file. But that was just a small bottleneck which was solved by replacing double quotes (") with single quotes (') and comma (,) with (&) characters. Thus for the above case, the data in the CSV file would have looked like this :
{'itemId':{'id':23}&'itemType':'BOOK'&'description':'batMAN'}
{'itemId':{'id':24}&'itemType':'JOURNAL'&'description':'SPIDERMAN'}
{'itemId':{'id':25}&'itemType':'E-BOOK'&'description':'SUPERMAN'}
rest all would have remained the same.
In case you have any questions about JSON and EasyTest in particular or about EasyTest in general please mail me at anujkumar@easetech.org
For any queries/issues/clarifications please contact anujkumar@easetech.org