You need to sign in to do that
Don't have an account?
Komal Wagh
Unable to cover the code in the test class.
I
This is my Apex class public class ExportEmailMessageDataController { Public Static List<Case> documents{get;set;} Public Static List<EmailMessage> Doc{get;set;} public ExportEmailMessageDataController(ApexPages.StandardSetController controller){ } public Static Void getEmailMessage(){ documents = [SELECT Id, (SELECT Id, ParentId, TextBody, Headers, Subject, FromName, FromAddress, ToAddress, BccAddress,CcAddress, Incoming, HasAttachment, Status FROM EmailMessages)From Case Where Status = 'Closed']; // List<EmailMessage> em =[SELECT LastModifiedDate, Id, Subject,ParentId FROM EmailMessage where LastModifiedDate <= LAST_N_DAYS:30 ]; system.debug('documents'+documents); String replaceBr = ''; Doc = new List<EmailMessage>(); for(Case c : documents){ system.debug('c.EmailMessages'+c.EmailMessages); if(c.EmailMessages!=null){ system.debug('In email message'); for(EmailMessage em : c.EmailMessages){ system.debug('check email message'); if( em.TextBody != NULL) { em.TextBody = em.TextBody.replace('\r\n', ' '); em.TextBody = em.TextBody.replace('\n', ' '); em.TextBody = em.TextBody.replace('\r', ' '); } Doc.add(em); } } } } } //This is the test class @istest public class TestExportEmailMessageDataController { public Static List<EmailMessage> Doc{get;set;} @istest public static void testFuction(){ test.startTest(); Doc = new List<EmailMessage>(); List<EmailMessage> emailList = new List<EmailMessage>(); List<Case> caseList = new List<Case>(); Case c = new Case(); c.Subject = 'Om Test'; c.Status ='Closed'; c.Priority = 'Medium'; c.Origin = 'Email'; insert c; Case c1 = new Case(); c1.Subject = 'Om Test'; c1.Status ='Closed'; c1.Priority = 'Medium'; c1.Origin = 'Email'; insert c1; caseList.add(c); caseList.add(c1); //Insert emailmessage for case EmailMessage email = new EmailMessage(); email.FromAddress = 'test@abc.org'; email.Incoming = true; email.ToAddress= 'test@xyz.org'; email.Subject = 'Test email'; email.HtmlBody = 'Test email body'; email.ParentId = c.Id; email.TextBody = 'test test test'; email.RelatedToId =c.Id; email.CcAddress = 'deepak.potghan@aress.com'; email.BccAddress = 'deepak.potghan@aress.com'; email.Status = '1'; insert email; system.debug('Inserted email'+email); ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(caseList); ExportEmailMessageDataController ext = new ExportEmailMessageDataController(stdSetController); ExportEmailMessageDataController.getEmailMessage(); test.stopTest(); } }
Your test code is fine make a small change like below. You will get 100% code coverage. I just tested it.
Please change to System.Test.startTest(); instead of test.startTest(); and also for System.Test.stopTest(); instead of test.stopTest();.
Regards,
SalesforceLearner