Skip to main content
Trevor Wilson preguntó en #Apex
Hi All, 

I am very new to Apex and managed to kludge the below code together from wonderful examples found around here. It works great in my sandbox and pre-prod but I'm having a really hard time figuring out how to write a test class for it so I can move to prod. I've followed the trailhead and lots and lots of similar posts but all I can get is 0% code coverage. 

Long story short: Apex class looks in a picklist for a list of emails, gets the subject from a VF page (picklist) and body of email from the same VF page customers fill out. Then it sends the email to everyone in the picklist. Any help is GREATLY appreciated! Been working on this for weeks scouring documents and getting no where. 

Apex Class

public class KnowledgeEmailer{

public String KBArticles { get; set; }

public string message{get;set;}

public String body { get; set; }

//grab the picklist values for to send the email to

public List<String> myPicklist {

get {

if (myPicklist == null) {

myPicklist = new List<String>();

Schema.DescribeFieldResult field = UiKnowledgeEmailObject__c.ContentManager__c.getDescribe();

for (Schema.PicklistEntry f : field.getPicklistValues())

myPicklist.add(f.getLabel());

}

return myPicklist;

}

set;

}

//Grab the picklist values we just pulled from the object-field and process the list

String toAddresses = string.join(myPicklist, ',');

//Grab the picklist for the topic which the user selects and will go into the email as the subject line

public string selectValue { get;set; }

public List<SelectOption> statusOptions { get;set; }

public void autoRun()

{

Schema.DescribeFieldResult statusFieldDescription = UiKnowledgeEmailObject__c.UiKnowledgeTopic__c.getDescribe();

statusOptions = new list<SelectOption>();

for (Schema.Picklistentry picklistEntry : statusFieldDescription.getPicklistValues())

{

statusOptions.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));

}

}

public PageReference send() {

// Define the email

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

// Split the email addresses from the picklist field to go into the To field for email sending

String[] toAddresses = toAddresses.split(',', 0);

// Sets the paramaters of the email

email.setSubject( selectValue );

email.setToAddresses( toAddresses );

email.setPlainTextBody( body );

// Sends the email

Messaging.SendEmailResult [] r =

Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

// Message displayed to user

message='Your request has been sent to a Content Manager';

return null;

}

}

Test class that gets 0% coverage

@isTest

private class Test_KnowledgeEmailer {

@isTest static void loadPicklist(){

//tried various methods here and no joy

}

@isTest static void loadTopic(){

//tried various methods here and no joy

}

@isTest static void loadBody(){

//tried various methods here and no joy

}

@isTest static void sendEmail() {

String description = 'Test Description';

String title = 'Test Title';

List<String> ccAddress = new List<String>{'test_1@gmail.com'};

List<String> toAddress = new List<String>{'test2@gmail.com'};

EmailService.sendEmail(description,title,ccAddress,toAddress);

}

}

 
3 respuestas
  1. 25 jun 2021, 16:47

    Hi,

    Please find the solution with 66% coverage.

    public class KnowledgeEmailer{

    //create constructor

    public KnowledgeEmailer(){

    }

    public String KBArticles { get; set; }

    public string message{get;set;}

    public String body { get; set; }

    //grab the picklist values for to send the email to

    public List<String> myPicklist {

    get {

    if (myPicklist == null) {

    myPicklist = new List<String>();

    Schema.DescribeFieldResult field = UiKnowledgeEmailObject__c.ContentManager__c.getDescribe();

    for (Schema.PicklistEntry f : field.getPicklistValues())

    myPicklist.add(f.getLabel());

    }

    return myPicklist;

    }

    set;

    }

    //Grab the picklist values we just pulled from the object-field and process the list

    String toAddresses = string.join(myPicklist, ',');

    //Grab the picklist for the topic which the user selects and will go into the email as the subject line

    public string selectValue { get;set; }

    public List<SelectOption> statusOptions { get;set; }

    public void autoRun()

    {

    Schema.DescribeFieldResult statusFieldDescription = UiKnowledgeEmailObject__c.UiKnowledgeTopic__c.getDescribe();

    statusOptions = new list<SelectOption>();

    for (Schema.Picklistentry picklistEntry : statusFieldDescription.getPicklistValues())

    {

    statusOptions.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));

    }

    }

    public PageReference send() {

    // Define the email

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

    // Split the email addresses from the picklist field to go into the To field for email sending

    String[] toAddresses = toAddresses.split(',', 0);

    // Sets the paramaters of the email

    email.setSubject( selectValue );

    email.setToAddresses( toAddresses );

    email.setPlainTextBody( body );

    // Sends the email

    Messaging.SendEmailResult [] r =

    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

    // Message displayed to user

    message='Your request has been sent to a Content Manager';

    return null;

    }

    }

    Test Class :

    @isTest

    public class KnowledgeEmailerTest {

    public static testMethod void forTest(){

    List<SelectOption> statusOptions=new List<SelectOption>();

    test.startTest();

    KnowledgeEmailer obj=new KnowledgeEmailer();

    List<String> myPixkListData=obj.myPicklist;

    obj.KBArticles='test';

    obj.statusOptions=statusOptions;

    obj.message='data';

    obj.send();

    obj.autoRun();

    test.stopTest();

    }

    Do some needful changes to get the 75% coverage.

    Please mark it as the Best Answer so that other people would take references from it.

    Thank You

0/9000