You need to sign in to do that
Don't have an account?

Help me with the code coverage
Hello All,
I have class which is used to fetch attachment and sending an email.I wrote the test class but it is shwoing only 28% code coverage.Can any one please help me to cover this code.
=================class=======================
public class sendEmailtoContact {
public String subject {
get;
set;
}
public String body {
get;
set;
}
private final Account account;
public List < String > ToemailAddresses = new List < String > ();
public List < contact > lsc = new List < contact > ();
public sendEmailtoContact() {}
public PageReference send() {
User Usr = new User();
Usr = [SELECT country FROM User WHERE Id = : UserInfo.getUserId()];
lsc = [select id, name, Email, CustomDeclarationLetterDE__c, CustomDeclarationLetterUK__c, CustomDeclarationLetterItaly__c, CustomDeclarationLetterFr__c from contact where CustomDeclarationLetterDE__c = true or CustomDeclarationLetterUK__c = true or CustomDeclarationLetterItaly__c = true or CustomDeclarationLetterFr__c = true];
System.debug('List of contacts retrieved' + lsc);
List < Messaging.SingleEmailMessage > mails = new List < Messaging.SingleEmailMessage > ();
for (contact cs: lsc) {
ToemailAddresses.add(cs.email);
system.debug('list of ToemailAddresses information' + ToemailAddresses);
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
PageReference pdf = Page.CustomDeclarationLetter;
pdf.getParameters().put('id', cs.id);
pdf.setRedirect(true);
// need to pass unit test -- current bug
// Take the PDF content
Blob b = pdf.getContent();
// Create the email attachment
String subject1='Long-Term ';
String Htmlbody= 'test123'
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('CustomDeclarationLetter.pdf');
efa.setBody(b);
email.setSubject(subject1);
email.setOrgWideEmailAddressId('0D2200000004EQx');
email.setHtmlBody(Htmlbody);
email.setTargetObjectId(cs.id);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
mails.add(email);
// Sends the email
//Based on the current logged in user the respective checkbox will be updated to false
if (Usr.country == 'Germany') {
cs.CustomDeclarationLetterDE__c = false;
} else if (Usr.country == 'UK') {
cs.CustomDeclarationLetterUK__c = false;
} else if (Usr.country == 'Italy') {
cs.CustomDeclarationLetterItaly__c = false;
} else {
cs.CustomDeclarationLetterFr__c = false;
}
}
if (!mails.isEmpty()) {
Messaging.sendEmail(mails);
update lsc;
}
return null;
}
}
============class=============================
===========================test class========================================
@isTest
public class TestsendEmailtoContact{
public static testMethod void testSendemailmessageContact()
{
Profile p = [SELECT Id FROM profile WHERE name='System Administrator'];
User u = new User(alias = 'MTstCN', email='newuser@testorg.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,
timezonesidkey='Europe/Berlin', username='m_tstComplaintNum@tstorg20110329.tst');
insert u;
system.runAs(u){
//Create an Account of Record Type Customer
Schema.DescribeSObjectResult da = Schema.SObjectType.Account;
Map<String,Schema.RecordTypeInfo> rtAMapByName = da.getRecordTypeInfosByName();
Schema.RecordTypeInfo rtInfoMInt= rtAMapByName.get('Customer');
ID accCustomerRTypeID = rtInfoMInt.getRecordTypeId();
Account a = new Account(Name='M_Tst_ComplaintNumber') ;
a.OwnerId=u.ID;
a.RecordTypeID=label.Account_Mauser_Record_TypeID;
insert a;
system.debug('Account ID:' + a.ID);
Contact ct = new Contact();
ct.AccountId=a.ID;
ct.LastName='Test contact';
ct.Email='Test@test.com';
ct.RecordTypeId=Label.Mauser_Contact_Rt;
ct.Inactive_Contact__c=false;
insert ct;
email.plainTextBody = 'This should become a note';
email.fromAddress ='test@test.com';
String contactEmail = 'jsmith@salesforce.com';
email.ccAddresses = new String[] {'Jon Smith <' + contactEmail + '>'};
email.subject = 'Dummy Account Name 123';
PageReference pref = Page.CustomDeclarationLetter;
pref.getParameters().put('id',ct.id);
Test.setCurrentPage(pref);
sendEmailtoContact con = new sendEmailtoContact();
//------------------- START TEST-------------------
Test.startTest();
// submit the record
pref = con.send(ct.id);
Test.stopTest();
}
}
}
===============================================test class===================
I have class which is used to fetch attachment and sending an email.I wrote the test class but it is shwoing only 28% code coverage.Can any one please help me to cover this code.
=================class=======================
public class sendEmailtoContact {
public String subject {
get;
set;
}
public String body {
get;
set;
}
private final Account account;
public List < String > ToemailAddresses = new List < String > ();
public List < contact > lsc = new List < contact > ();
public sendEmailtoContact() {}
public PageReference send() {
User Usr = new User();
Usr = [SELECT country FROM User WHERE Id = : UserInfo.getUserId()];
lsc = [select id, name, Email, CustomDeclarationLetterDE__c, CustomDeclarationLetterUK__c, CustomDeclarationLetterItaly__c, CustomDeclarationLetterFr__c from contact where CustomDeclarationLetterDE__c = true or CustomDeclarationLetterUK__c = true or CustomDeclarationLetterItaly__c = true or CustomDeclarationLetterFr__c = true];
System.debug('List of contacts retrieved' + lsc);
List < Messaging.SingleEmailMessage > mails = new List < Messaging.SingleEmailMessage > ();
for (contact cs: lsc) {
ToemailAddresses.add(cs.email);
system.debug('list of ToemailAddresses information' + ToemailAddresses);
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
PageReference pdf = Page.CustomDeclarationLetter;
pdf.getParameters().put('id', cs.id);
pdf.setRedirect(true);
// need to pass unit test -- current bug
// Take the PDF content
Blob b = pdf.getContent();
// Create the email attachment
String subject1='Long-Term ';
String Htmlbody= 'test123'
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('CustomDeclarationLetter.pdf');
efa.setBody(b);
email.setSubject(subject1);
email.setOrgWideEmailAddressId('0D2200000004EQx');
email.setHtmlBody(Htmlbody);
email.setTargetObjectId(cs.id);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
mails.add(email);
// Sends the email
//Based on the current logged in user the respective checkbox will be updated to false
if (Usr.country == 'Germany') {
cs.CustomDeclarationLetterDE__c = false;
} else if (Usr.country == 'UK') {
cs.CustomDeclarationLetterUK__c = false;
} else if (Usr.country == 'Italy') {
cs.CustomDeclarationLetterItaly__c = false;
} else {
cs.CustomDeclarationLetterFr__c = false;
}
}
if (!mails.isEmpty()) {
Messaging.sendEmail(mails);
update lsc;
}
return null;
}
}
============class=============================
===========================test class========================================
@isTest
public class TestsendEmailtoContact{
public static testMethod void testSendemailmessageContact()
{
Profile p = [SELECT Id FROM profile WHERE name='System Administrator'];
User u = new User(alias = 'MTstCN', email='newuser@testorg.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,
timezonesidkey='Europe/Berlin', username='m_tstComplaintNum@tstorg20110329.tst');
insert u;
system.runAs(u){
//Create an Account of Record Type Customer
Schema.DescribeSObjectResult da = Schema.SObjectType.Account;
Map<String,Schema.RecordTypeInfo> rtAMapByName = da.getRecordTypeInfosByName();
Schema.RecordTypeInfo rtInfoMInt= rtAMapByName.get('Customer');
ID accCustomerRTypeID = rtInfoMInt.getRecordTypeId();
Account a = new Account(Name='M_Tst_ComplaintNumber') ;
a.OwnerId=u.ID;
a.RecordTypeID=label.Account_Mauser_Record_TypeID;
insert a;
system.debug('Account ID:' + a.ID);
Contact ct = new Contact();
ct.AccountId=a.ID;
ct.LastName='Test contact';
ct.Email='Test@test.com';
ct.RecordTypeId=Label.Mauser_Contact_Rt;
ct.Inactive_Contact__c=false;
insert ct;
email.plainTextBody = 'This should become a note';
email.fromAddress ='test@test.com';
String contactEmail = 'jsmith@salesforce.com';
email.ccAddresses = new String[] {'Jon Smith <' + contactEmail + '>'};
email.subject = 'Dummy Account Name 123';
PageReference pref = Page.CustomDeclarationLetter;
pref.getParameters().put('id',ct.id);
Test.setCurrentPage(pref);
sendEmailtoContact con = new sendEmailtoContact();
//------------------- START TEST-------------------
Test.startTest();
// submit the record
pref = con.send(ct.id);
Test.stopTest();
}
}
}
===============================================test class===================
Let us know if this will help you