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
hramanihramani 

Test Class Coverage Help - JSON

I have method, I need Test coverage for it. It contains JSON.createParser and I'm not sure how to cover all these Json in the test class,
Can you please help.

private void cardsInAppType(String strAppType)
  {
    List<Application_Process__c> apList = new List<Application_Process__c>();
    Application_Process__c ap = new Application_Process__c();
    formsList = new List<Form>();

     if (strAppType != NULL)
    {
      apList = [SELECT id, Name, Configuration__c, Reusable__c
            FROM Application_Process__c
            WHERE id = :strAppType LIMIT 1];

      if (apList.size() > 0)
      {
        ap = apList[0];

        String JSONContent = ap.Configuration__c;

        if (JSONContent != NULL)
        {
          JSONParser parser = JSON.createParser('{}');
          Form f = new Form(parser, ap.name, ap.id, ap.Reusable__c);

          try {
            parser = JSON.createParser(JSONContent);

            f = new Form(parser, ap.name, ap.id, ap.Reusable__c);
          }
          catch (System.JSONException e) {
            parser = JSON.createParser('{}');
            f = new Form(parser, ap.name + ' ---- Error in Configuration', ap.id, ap.Reusable__c);
            System.debug(' JSON Exception on : ERROR: ' + e.getMessage());
          }

          formsList.add(f);

          if (f.sectionsList != NULL && f.sectionsList.size() > 0)
          {
            for (Integer x = 0; x < f.sectionsList.size(); x++)
            {
              Section s = f.sectionsList[x];

              if (s.cardsList != NULL && s.cardsList.size() > 0)
              {
                for (Integer y = 0; y < s.cardsList.size(); y++)
                {
                  Card c = s.cardsList[y];

                  if (c.formId != NULL)
                  {
                    List<Application_Process__c> cardList = new List<Application_Process__c>();
                    Application_Process__c card = new Application_Process__c();
                    cardList = [SELECT id, Name, Configuration__c, Reusable__c
                          FROM Application_Process__c
                          WHERE id = :c.formId LIMIT 1];

                    if (cardList.size() > 0)
                    {
                      addFormsTolist(cardList);

                      /*card = cardList[0];
                      
                      String JSONContent2 = card.Configuration__c;

                      if (JSONContent2 != NULL)
                      {
                        JSONParser parser2 = JSON.createParser(JSONContent2);
                        formsList.add(new Form(parser2, card.name, card.id, card.Reusable__c));
                      }*/
                    }
                    else
                    {
                      s.cardsList[y].label = s.cardsList[y].label + ' ---- Form Does Not Exist';
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
Naveen KNNaveen KN
Try sending test JSON data to your method. I see that you are sending APP type input parameter to the method, make sure you feed json content to the particular lines of the Apex from your test class.

If the below-mentioned criteria satisfied, parser logic gets covered.

 if (JSONContent != NULL)

- Naveen K N