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
Michael MMichael M 

Why isn't test code covering apex class?

Hello, I have apex class like this:

public class TwilioChatterConversationClass {
public static void ContactPostChatter(Id contactRecordId, string message){  
        if (flag == true){
        flag = false;
        Contact c = [ Select id, name, related_referral__r.ownerid, related_referral__r.name, related_Referral__r.Referral_Account__r.name From Contact Where Id =: contactRecordId];

        ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
        ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
        ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
        ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();

        messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
        
        mentionSegmentInput.id = c.related_referral__r.ownerid;
    
//hyperlink        
String fullFileURL = URL.getSalesforceBaseUrl().toExternalForm()+'/'+ c.id;   
ConnectApi.LinkCapabilityInput linkInput = new ConnectApi.LinkCapabilityInput();
linkInput.url = fullFileURL;
linkInput.urlName = c.name;
ConnectApi.FeedElementCapabilitiesInput feedElementCapabilitiesInput = new ConnectApi.FeedElementCapabilitiesInput();
feedElementCapabilitiesInput.link = linkInput;
feedItemInput.capabilities = feedElementCapabilitiesInput; 
//

        messageBodyInput.messageSegments.add(mentionSegmentInput);

 //  textSegmentInput.text = 'Referral, "' + c.related_referral__r.name  +  '".         Next of Kin,' + c.name +', has responded to your text: ' + message + ' To view their record <a href=https://centers.lightning.force.com/'+c.Id+'>click here.</a>' ;
   textSegmentInput.text = 'Referral, "' + c.related_referral__r.name  +  '".         Next of Kin,' + c.name +', has responded to your text: ' + message;
    messageBodyInput.messageSegments.add(textSegmentInput);

        feedItemInput.body = messageBodyInput;
        feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
        feedItemInput.subjectId = c.related_referral__r.ownerid;
        
    
ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput);
    return;
}
}
}
}

I'm trying test code like this, but it's not covering the class (see screenshot below). Any idea why not?

@isTest
public class TwilioTriggerTest {
@isTest
    static void TwilioTest(){
        contact c = new contact();
        c.lastname = 'Test';
        c.MobilePhone = '123456789';
        c.Responded_to_first_message__c = true;
        insert c;

        TwilioChatterConversationClass.ContactPostChatter(c.id, 'hey');
}}

User-added image
 
Best Answer chosen by Michael M
sachinarorasfsachinarorasf
Hi Michael,

There is an if the condition on the flag variable and in your apex class I am not able to see its initialization. you have to set its value true. And also create Related_Referral__c object record in your test class.

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com

All Answers

AbhishekAbhishek (Salesforce Developers) 
Hi,

Increase code coverage

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

75 %

https://salesforce.stackexchange.com/questions/24165/why-is-75-code-coverage-required-in-salesforce/24167#24167

This will help you, Check it once.


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
sachinarorasfsachinarorasf
Hi Michael,

There is an if the condition on the flag variable and in your apex class I am not able to see its initialization. you have to set its value true. And also create Related_Referral__c object record in your test class.

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
This was selected as the best answer
Michael MMichael M
Thank you!!