You need to sign in to do that
Don't have an account?
sumit 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?
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?
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
Modify your tets class as below, regards
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?