+ Start a Discussion
Shubham Sinha 49Shubham Sinha 49 

How to write Test class for JSON.deserialise

I have a class where we have used JSON.deserialise function but that function is not being covered by test . I have tried multiple things but no luck. Can anyone please help.
for(ACN_NavigationLibrary.NavigationListItem topicItem : topicsList.Items) {
            ACN_NavigationLibrary.NavigationListItem groupItem = (ACN_NavigationLibrary.NavigationListItem)JSON.deserialize(JSON.serialize(topicItem),ACN_NavigationLibrary.NavigationListItem.class);
            topicItem.IsGroupableItem = true;
            groupItem.Id = topicItem.Name;
            groupItem.Code = topicItem.Name;
            groupItem.GroupName = topicItem.Name;
            groupItem.StyleClassName = 'biib-adu-body-fontsize biib-looking-for-treatment-info-color biib-adu-portal-base-font-family *' ;
            groupItem.NavigationType = 'Event';
            groupItem.NavigationTarget = 'navigationListGroupToggleVisibilityRequested';
            groupItem.NavigationTargetVariable = new Map<String,String>();
            groupItem.SerializedNavigationTargetVariable = '{"navigationItem":{!navigationItem}}';
            //groupItem.IsGroupExpanded = true;
            topicNameList.add(groupItem.GroupName);

            navList.GroupItems.put(groupItem.GroupName, groupItem);
            
            ACN_NavigationLibrary.NavigationRequest articleRequest = (ACN_NavigationLibrary.NavigationRequest)JSON.deserialize(JSON.serialize(request),ACN_NavigationLibrary.NavigationRequest.class);
            articleRequest.Id = topicItem.Name;
            articlesList = articlesProvider.GetNavigationList(articleRequest);
            
            for (ACN_NavigationLibrary.NavigationListItem articleItem : articlesList.Items) {
                ACN_NavigationLibrary.NavigationListItem navItem = (ACN_NavigationLibrary.NavigationListItem)JSON.deserialize(JSON.serialize(articleItem),ACN_NavigationLibrary.NavigationListItem.class);
                navItem.GroupName = groupItem.Name;
                navItem.StyleClassName = 'biib-article-accordion-margin-right biib-margin-left-negetive-7px biib-adu-portal-base-font-family * biib-adu-embedded-link-fontsize biib-font-bold  biib-phone-no-color';
                //navItem.NavigationType = '';
                //navItem.NavigationTarget = '';
                //navItem.NavigationTargetVariables = '';
                navList.Items.add(navItem);
            }
            
        }
AnudeepAnudeep (Salesforce Developers) 
You need to use Test.setMock to create a fake response in your test class because this involves a callout. I recommend looking at the example listed here.

If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!

Anudeep