function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashok S 7Ashok S 7 

how can i write the public static JSON2Apex parse(String json) in the testclass

Here my requirement is how to use the  
public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
        }
in my test class
my test class is following here
@istest(seeAllData = true)
public class allPatntsRcrdsCtrlr_Test
{
    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
        }
   public static testmethod void test()
   {
      test.startTest();
     //inserting a record into the Account object
     Account acc = new Account();
     acc.Name = 'Sample Account';
     insert acc;
     
     //Inserting a Record into contact object
     Contact c = new Contact();
     c.Accountid = acc.id;
     c.Department = 'Testing';
     c.LastName = 'Test';
     insert c;
     
     //inserting a record into the Paitent object
     Patient__c pt = new Patient__c();
     pt.BlueStarID__c = 'fdsrt1224565';
     pt.Name = 'Sample'; 
     pt.Patient_First_Name__c = 'Test'; 
     pt.Patient_Id__c= 'sder45789'; 
     pt.Patient_Last_Name__c='Sample1'; 
     pt.ExternalID__c= 'sader22244578'; 
     pt.ProviderName__c= ''; 
     pt.SRType__c= 'Yes'; 
     pt.SampleCode__c= 'No'; 
     pt.ServiceRequestID__c= 12456789; 
     pt.UserID__c= 2456789;
     insert pt;
     //calling a pagereference in testclass
     PageReference pg = page.allPatientRecords;
     pg.getparameters().put('id',string.valueOf(pt.id));
     test.setcurrentpage(pg); 
     
     //calling the controller name
     allPatntsRcrdsCtrlr aprc = new allPatntsRcrdsCtrlr();
     //calling wrapperclass in test class
     
     
     //aprc.patlist  = pt;
     aprc.strBody = 'Plain';
     aprc.idv = 'Test';
     aprc.searchVal = '';
     aprc.searchFields = 'ProviderName';
     aprc.labeling = 'ProviderName';
     aprc.show = false;
     aprc.lstrecdstb1 = false;
     aprc.queryflds = true;
     aprc.pdfrendered = false;
     aprc.showmessage = true;
     aprc.wordRendered = true;
     aprc.xlRendered  = true;
     //calling methods in testcalss
     
      allPatntsRcrdsCtrlr aprc1 = new allPatntsRcrdsCtrlr();
     //calling wrapperclass in test class
     
     
     //aprc.patlist  = pt;
     aprc1.strBody = 'Rectangle';
     aprc1.idv = 'Test';
     aprc1.searchVal = '';
     aprc1.searchFields = 'BlueStarID';
     aprc1.labeling = 'BlueStarID';
     aprc1.show = false;
     
     aprc1.lstrecdstb1 = false;
     aprc1.queryflds = true;
     aprc1.pdfrendered = false;
     aprc1.showmessage = true;
     aprc1.wordRendered = true;
     aprc1.xlRendered  = true;
     
     allPatntsRcrdsCtrlr aprc2 = new allPatntsRcrdsCtrlr();
     //calling wrapperclass in test class
     
     
     //aprc.patlist  = pt;
     aprc2.strBody = 'Square';
     aprc2.idv = 'Test';
     aprc2.searchVal = 'Test';
     aprc2.searchFields = 'Patient_First_Name__c';
     aprc2.labeling = 'Patient_First_Name__c';
     aprc2.show = false;
     /*
     aprc2.lstrecdstb1 = false;
     aprc2.queryflds = true;
     aprc2.pdfrendered = true;
     aprc2.showmessage = true;
     aprc2.wordRendered = true;
     aprc2.xlRendered  = true;
     */
     allPatntsRcrdsCtrlr aprc3 = new allPatntsRcrdsCtrlr();
     //calling wrapperclass in test class
     
     
     //aprc.patlist  = pt;
     aprc3.strBody = 'square';
     aprc3.idv = 'Test';
     aprc3.searchVal = 'Sample1';
     aprc3.searchFields = 'Patient_Last_Name__c';
     aprc3.labeling = 'Patient_Last_Name__c';
     aprc3.show = false;
     
     aprc3.lstrecdstb1 = false;
     aprc3.queryflds = true;
     aprc3.pdfrendered = false;
     aprc3.showmessage = true;
     aprc3.wordRendered = true;
     aprc3.xlRendered  = true;
     
     
     aprc.clear();
     //aprc.patinfo();
     //aprc.PatientData();
     aprc.retrieve();
     
     //aprc.doSearch();
     //aprc.ExpXL();
    
     aprc.ExpPDF();
     aprc.ExpWord();
     aprc.getlabeling();
     aprc.getsearchFields();
     
      allPatntsRcrdsCtrlr.oAuthRecords ar = new allPatntsRcrdsCtrlr.oAuthRecords();
          ar.BlueStarID = '';
          ar.HubCode    = 'asd145789jjhgdrt';
          ar.PatientName = 'Pointing';
          ar.PatientFirstName = 'John';
          ar.PatientID   = 'serer45789dfh';
          ar.PatientLastName = 'Ricky';
          ar.ProviderName   = '';
          
          ar.SRType = 'Active';
          ar.SampleCode = 'Yes';
          ar.ExternalID = '1245gggtder214578';
          ar.ServiceRequestID = 20001457;
          ar.UserID  = 457896321;
      allPatntsRcrdsCtrlr.JSON2Apex ja = new allPatntsRcrdsCtrlr.JSON2Apex();
      ja.access_token = 'hfert45778969jhgt';
     List<Apexpages.Message> pageMessages = ApexPages.getMessages();
     test.stopTest();
   }
}
KevinPKevinP
AShok,

This is one of those tedius things where it depends on the json data returned. This tool: https://json2apex.herokuapp.com/  (https://json2apex.herokuapp.com/)

Will generate the class & tests class from your json document.