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
sumit dsumit d 

code coverage for test class

    Hi All,
    my helper class is given below-
 //Helper class for Trigger_SocialPost
public without sharing class SocialPostTriggerHelper {
    public static List<SocialPost> newList;
    public static List<SocialPost> oldList;
    public static Map<Id, SocialPost> newMap;
    public static Map<Id, SocialPost> oldMap;
    
    public static Boolean runTrigger = true;
    
    public static void parseImageResponse(){
        Set<Id> socialPostIds = new Set<Id>(newMap.keySet());
        List<contentdocumentlink> cdlList = new List<contentdocumentlink>([Select id, 
                                                                              LinkedEntityId 
                                                                              from contentdocumentlink 
                                                                              where LinkedEntityId IN: socialPostIds]);
        List<Predictions__c> pdcList = new List<Predictions__c>();
        for(contentdocumentlink cdl : cdlList){
            if(newMap.containsKey(cdl.LinkedEntityId)){
               String jsonResponse = EinsteenPredictionService.getImagePrediction('test');
               EinsteenPredictionService ep= EinsteenPredictionService.parseImagePrediction(jsonResponse);
                for(EinsteenPredictionService.cls_probabilities result: ep.probabilities) {
                    Predictions__c    pc = new Predictions__c();
                    pc.Label__c = result.label;
                    pc.Probability__c = result.probability;
                    pc.ReferencetoSocialPost__c = cdl.LinkedEntityId;
                    pdcList.add(pc);
                }
               
            }
        }
        if(pdcList.size() > 0){
           insert pdcList; 
        }
        
    }
}
my test class is given below:-
@isTest
public class SocialPostTriggerHelperTest {
     @isTest static void SocialPostTriggerHelperTestmethod(){
        SocialPostTriggerHelper sth = new SocialPostTriggerHelper();
        SocialPostTriggerHelper.parseImageResponse();
        SocialPost sp = new SocialPost();
         
         Map<Id, SocialPost> newMap = new Map <Id, SocialPost>();
         newMap.put(sp.id,sp);
         insert sp;
         EinsteenPredictionService.cls_probabilities ein = new EinsteenPredictionService.cls_probabilities();
         ein.label = 'nachos';
         ein.probability = 0.9994034;
         
         contentdocumentlink cdl = new contentdocumentlink();
         
                    Predictions__c pc = new Predictions__c();
                    pc.Label__c = ein.label;
                    pc.Probability__c = ein.probability;
                    pc.ReferencetoSocialPost__c = cdl.LinkedEntityId;
         test.startTest();
         insert pc;
         test.stopTest();
     }
}
its showing this error:-System.NullPointerException: Attempt to de-reference a null object
                        Class.SocialPostTriggerHelper.parseImageResponse: line 11, column 1
                        Class.SocialPostTriggerHelperTest.SocialPostTriggerHelperTestmethod: line 5, column
     how to solve this error and cover the code?
Any suggestion?
sumit dsumit d
User-added image
Sandeep YadavSandeep Yadav
Hi Sumit,
you have to pass a List<sObject> records for your helper class method.
and your method should be like this--
 public static void parseImageResponse(List<sObject>){
    //your code
 }
That's why your query for contentdocumentlink is not returning any data.
And you have also to create test data for ContentDocumentLink object.
you are assigning null value to ReferencetoSocialPost__c field 
pc.ReferencetoSocialPost__c = cdl.LinkedEntityId; // null value
Sampath SuranjiSampath Suranji
Hi,
Modify your tets class as below,
................................................................
        SocialPost sp = new SocialPost();// if there are required fields, then provide them also
        insert sp;
  
         Map<Id, SocialPost> newMap = new Map <Id, SocialPost>();
         newMap.put(sp.id,sp);
         SocialPostTriggerHelper.newMap= newMap;
          SocialPostTriggerHelper.parseImageResponse();
.........................................................................
regards
 
sumit dsumit d
Hi,
Can you provide me the test class?
my test class is  still failing below:-
@isTest
public class SocialPostTriggerHelperTest {
     @isTest static void SocialPostTriggerHelperTestmethod(){
        SocialPostTriggerHelper sth = new SocialPostTriggerHelper();
        SocialPost sp = new SocialPost(Name= 'image');
         
         Map<Id, SocialPost> newMap = new Map <Id, SocialPost>();
         newMap.put(sp.id,sp);
         SocialPostTriggerHelper.newMap= newMap;

         insert sp;
         EinsteenPredictionService.cls_probabilities ein = new EinsteenPredictionService.cls_probabilities();
         ein.label = 'nachos';
         ein.probability = 0.9994034;
         ContentVersion cv=new ContentVersion(); 
         cv.title = 'test content trigger';      
        cv.PathOnClient ='test'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
            cv.VersionData=bodyBlob;         
        insert cv;         
         contentdocumentlink cdl = new contentdocumentlink();
         cdl.LinkedEntityId=sp.id;
         cdl.contentdocumentid=[select contentdocumentid from contentversion where id =: cdl.id].contentdocumentid;

         insert cdl;
                    Predictions__c pc = new Predictions__c();
                    pc.Label__c = ein.label;
                    pc.Probability__c = ein.probability;
                    pc.ReferencetoSocialPost__c = cdl.LinkedEntityId;
                    SocialPostTriggerHelper.parseImageResponse();

         test.startTest();
         insert pc;
         test.stopTest();
     }
}
System.UnexpectedException: ContentPublication Limit exceeded.
 how to solve it?
sumit dsumit d
User-added imagehow to cover this?