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);
}
}
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