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
Sebastian PageSebastian Page 

I am facing issue to write test class

Hi All, 

I am facing issue to write test class for this code please help me i am stuck from one month please help me.

public class CommenttoEmail {
   
    public String Commentbody {get;set;}
   
    public String Set_to{get;set;}
    Public String Set_bcc{get;set;}
    public String Set_cc{get;set;}
    public List<String> SetTo {get;set;}
    public List<String> SetCC {get;set;}
    public List<String>  SetBCC{get;set;}
    Public String CaseId {get;set;}
   
    public  String Selec {get;set;}
    public List<String> liststatus {get;set;}
    public boolean flag {set;get;}
    public string Str2;
    
    public String test;
    Public List<string> listall {get;set;} 
     public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }
    public CommenttoEmail(apexpages.StandardController std)
    { 
        
                
        
        
        liststatus=new List<string>{'New','Escalated','On Hold','Action-Automated Escalation','Agent Research','Scheduled','Awaiting Customer Reply',
            'Awaiting External Response','Awaiting Part','Re-Opened','Closed Successful','Closed Unsuccessful'};
       
        flag=false;
       CaseId=ApexPages.CurrentPage().getParameters().get('Id');
        listall=new List<String>();
        SetTo=new List<String>{};
        SetCC=new  List<String>{};
        SetBCC=new  List<String>{}; 
    }

   
public void senttoEmail()
{
    Case cse = [select id ,CaseNumber ,Status,ContactEmail,subject from case where id=:caseId]; 
    
   
  SetTo=Set_To.split(',');
 

    
public void bccEmail()
{
  SetBCC=Set_bcc.split(',');
}
    
public void ccEmail()
{
    SetCC=Set_cc.split(',');

    public void ss()
    {
        senttoEmail();
        bccEmail();
        ccEmail();
    }
public void attachemail()
{
  listall.addAll(SetTo);
  listall.addAll(SetBCC);
  listall.addAll(SetCC);
  
}
    // this method used for creating  case comment
public pagereference  createCaseComment(){
    ss();
    upload();
attachemail();
sendingEmail();
ChangeStatus();

Case cse = [select id ,CaseNumber ,Status, Contact.name,Account.name,subject from case where id=:caseId];   
String ss=string.join(listall, ',');
String Str1='\n\n';
String Str0='[Recipients:' +ss+']';
String Str3=cse.Contact.name+',\n\n';
String  Reg='\n\n\n Sincerely,\n'+cse.Contact.name+'\n'+cse.Account.name;
   
Str2=Str0+Str1+Str3+Commentbody+Reg;

    
CaseComment cc = new CaseComment(ParentId = cse.Id,CommentBody = str2 , IsPublished=true);
insert cc;
  pagereference pg =new pagereference('/'+CaseId);

return pg;  
   
    }
    // this method used for sending email
public PageReference sendingEmail()
{
 upload();    
Case[] cse = [select id ,CaseNumber ,Status, Contact.name,Account.name,subject from case where id=:caseId limit 1];   
String ss=string.join(listall, ',');

String Str3=cse[0].Contact.name+',\n\n';
String  Reg='\n\n\n Sincerely,\n'+cse[0].Contact.name+'\n'+cse[0].Account.name;
   
Str2=Str3+Commentbody+Reg;
 List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
List<Attachment> Attlist=[select Name, Body, BodyLength from Attachment where ParentId = :caseId];
for (Attachment a :Attlist ){

Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();

efa.setFileName(a.Name);
efa.setBody(a.Body);
fileAttachments.add(efa);
}



    try{
  
Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
semail.setToAddresses(SetTo); 
semail.setBccAddresses(SetBCC); 
semail.setCcAddresses(SetCC); 
semail.setSubject(cse[0].Subject); 
semail.setPlainTextBody(Str2);
semail.setFileAttachments(fileAttachments);   
   
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail}); 
    
    
    }catch(exception e)
    {}
    return null;

   //This method use for redirect to  current ticket. 
public pagereference redirectCasePage()
{

CaseId=ApexPages.CurrentPage().getParameters().get('Id');
pagereference pg =new pagereference('/'+CaseId);

return pg;   
}
   //This Method used for change status 
    public void ChangeStatus()
    {
        
         List<Case> cse = [select id ,CaseNumber ,Status,subject from case where id=:caseId]; 
        for(Case c:cse){
            if(selec=='New')
            {
                c.Status='New';
            }
            
            if(selec=='Escalated')
            {
                c.Status='Escalated';
            }
            if(selec=='On Hold')
            {
                c.Status='On Hold';
            }
            if(selec=='Action-Automated Escalation')
            {
                c.Status='Action-Automated Escalation';
            }
            
            if(selec=='Agent Research')
            {
                c.Status='Agent Research';
            }
            
            if(selec=='Scheduled')
            {
                c.Status='Scheduled';
            }
            if(selec=='Action-Automated Escalation')
            {
                c.Status='Action-Automated Escalation';
            }
            
            if(selec=='Awaiting Customer Reply')
            {
                c.Status='Awaiting Customer Reply';
            }
            if(selec=='Awaiting External Response')
            {
                c.Status='Awaiting External Response';
            }
            if(selec=='Awaiting Part')
            {
                c.Status='Awaiting Part';
                
            }if(selec=='Re-Opened')
            {
                c.Status='Re-Opened';
            }if(selec=='Closed Successful')
            {
                c.Status='Closed Successful';
                
            }if(selec=='Closed Unsuccessful')
            {
                c.Status='Closed Unsuccessful';
            }
            update c;
        }
           }
// This Method create for status display in custom picklist       
public List<SelectOption> getCaseStatus()
    {
         List<SelectOption> optlist=new List<SelectOption>();
         Case[] cse = [select id ,CaseNumber ,Status,subject from case where id=:caseId limit 1];
       optlist.add(new SelectOption(cse[0].Status,cse[0].Status));
        for(String s:liststatus)
        {
           optlist.add(new SelectOption(s,s));
       
        }    
     return optlist;
    }
    
  //This Method Created for  uploading file 
   public PageReference upload() {
  
    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = CaseId;
    attachment.IsPrivate = true;
    attachment.ContentType = 'image/jpeg';
  
    try {
      insert attachment;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      attachment = new Attachment(); 
    }
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
  }
   
}
Best Answer chosen by Sebastian Page
Raj VakatiRaj Vakati
Try this code
 
@isTest
public class CommenttoEmailTest {
    
    static testMethod void testOptions(){        
        Account testAccount = new Account(Name='Test Company Name123');
        
        insert testAccount;
        
        
        Case caseObj = new Case(
            AccountId = testAccount.Id,
            Status = 'Working',
            Origin = 'Phone');
        
        insert caseObj;
        
        Attachment attach=new Attachment();   	
        attach.Name='Test';
        Blob bodyBlob=Blob.valueOf('Testing Body of Attachment');
        attach.body=bodyBlob;
        attach.parentId=caseObj.id;
        insert attach;
        
        
        ApexPages.StandardController sc = new ApexPages.StandardController(caseObj);
        CommenttoEmail testCon = new CommenttoEmail(sc);
        testCon.Commentbody='Working' ; 
        
        testCon.Set_to='asdghasd@asjdh.com,asdghasd@asjdh.com';
        testCon.Set_bcc='asdghasd@asjdh.com,asdghasd@asjdh.com';
        testCon.Set_cc='asdghasd@asjdh.com,asdghasd@asjdh.com';
        
        testCon.SetTo=new List<String>{'asdghasd@asjdh.com','asdghasd@asjdh.com'};
            testCon.SetCC=new List<String>{'asdghasd@asjdh.com','asdghasd@asjdh.com'};
                testCon.SetBCC=new List<String>{'asdghasd@asjdh.com','asdghasd@asjdh.com'};
                    testCon.CaseId = caseObj.Id ;
        testCon.Selec='Seclect';
        testCon.attachment = attach ; 
        PageReference pageRef = Page.AccountPlan;
        pageRef.getParameters().put('id', String.valueOf(caseObj.Id));
        Test.setCurrentPage(pageRef);
        
        testCon.senttoEmail();
        testCon.bccEmail();
        testCon.ccEmail();
        testCon.ss();
        testCon.attachemail();
        testCon.createCaseComment();
        testCon.sendingEmail();
        testCon.redirectCasePage();
        testCon.ChangeStatus();
        testCon.getCaseStatus();
        testCon.upload();
    }
}

 

All Answers

Raj VakatiRaj Vakati
Try this code
 
@isTest
public class CommenttoEmailTest {
    
    static testMethod void testOptions(){        
        Account testAccount = new Account(Name='Test Company Name123');
        
        insert testAccount;
        
        
        Case caseObj = new Case(
            AccountId = testAccount.Id,
            Status = 'Working',
            Origin = 'Phone');
        
        insert caseObj;
        
        Attachment attach=new Attachment();   	
        attach.Name='Test';
        Blob bodyBlob=Blob.valueOf('Testing Body of Attachment');
        attach.body=bodyBlob;
        attach.parentId=caseObj.id;
        insert attach;
        
        
        ApexPages.StandardController sc = new ApexPages.StandardController(caseObj);
        CommenttoEmail testCon = new CommenttoEmail(sc);
        testCon.Commentbody='Working' ; 
        
        testCon.Set_to='asdghasd@asjdh.com,asdghasd@asjdh.com';
        testCon.Set_bcc='asdghasd@asjdh.com,asdghasd@asjdh.com';
        testCon.Set_cc='asdghasd@asjdh.com,asdghasd@asjdh.com';
        
        testCon.SetTo=new List<String>{'asdghasd@asjdh.com','asdghasd@asjdh.com'};
            testCon.SetCC=new List<String>{'asdghasd@asjdh.com','asdghasd@asjdh.com'};
                testCon.SetBCC=new List<String>{'asdghasd@asjdh.com','asdghasd@asjdh.com'};
                    testCon.CaseId = caseObj.Id ;
        testCon.Selec='Seclect';
        testCon.attachment = attach ; 
        PageReference pageRef = Page.AccountPlan;
        pageRef.getParameters().put('id', String.valueOf(caseObj.Id));
        Test.setCurrentPage(pageRef);
        
        testCon.senttoEmail();
        testCon.bccEmail();
        testCon.ccEmail();
        testCon.ss();
        testCon.attachemail();
        testCon.createCaseComment();
        testCon.sendingEmail();
        testCon.redirectCasePage();
        testCon.ChangeStatus();
        testCon.getCaseStatus();
        testCon.upload();
    }
}

 
This was selected as the best answer
Sebastian PageSebastian Page
Hi  Raj ,

Thanks for reply  I have got 80% code coverage after some chnage your test code.
@isTest
public class CommenttoEmailTest {
    
    static testMethod void testOptions(){        
       
        Account testAccount = new Account(Name='Test Company Name123');
        
        insert testAccount;
        
         Contact con  = new Contact(LastName='Testcont' ,Email='sss@test.com', AccountId=testAccount.id);
        insert con;
        Case caseObj = new Case(
            AccountId = testAccount.Id,
            contactId=con.id,
            Ticket_category__c='Email',
            Ticket_Type__c='Login',
            Status = 'Action-Automated Escalation',
            Origin = 'Phone');
       test.startTest(); 
        insert caseObj;
        test.stopTest();
       
        
        
        Attachment attach=new Attachment();       
        attach.Name='Test';
        Blob bodyBlob=Blob.valueOf('Testing Body of Attachment');
        attach.body=bodyBlob;
        attach.parentId=caseObj.Id;
        
       
        
        
        ApexPages.StandardController sc = new ApexPages.StandardController(caseObj);
        CommenttoEmail testCon = new CommenttoEmail(sc);
        testCon.Commentbody='Working' ; 
        
        testCon.Set_to='asdghasd@asjdh.com,asdghasd@asjdh.com';
        testCon.Set_bcc='asdghasd@asjdh.com,asdghasd@asjdh.com';
        testCon.Set_cc='asdghasd@asjdh.com,asdghasd@asjdh.com';
        
        testCon.SetTo=new List<String>{'asdghasd@asjdh.com','asdghasd@asjdh.com'};
            testCon.SetCC=new List<String>{'asdghasd@asjdh.com','asdghasd@asjdh.com'};
                testCon.SetBCC=new List<String>{'asdghasd@asjdh.com','asdghasd@asjdh.com'};
                    testCon.CaseId = caseObj.Id ;
        testCon.Selec='New';
      testCon.attachment = attach ; 
        PageReference pageRef = Page.Mynew_commentvf;
        pageRef.getParameters().put('id', String.valueOf(caseObj.Id));
        Test.setCurrentPage(pageRef);
        
        testCon.senttoEmail();
        testCon.bccEmail();
        testCon.ccEmail();
        testCon.allEmailAddress();
        testCon.attachemail();
        testCon.createCaseComment();
        testCon.sendingEmail();
        testCon.redirectCasePage();
        testCon.ChangeStatus();
        testCon.getCaseStatus();
        testCon.upload();
      
    }
}