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
ChinnoduChinnodu 

I need code coverage morethan 75%, as of now i got 50% any one can hel me , below is my test class

Hi Experts,
Below is my test class ,
public with sharing class gboclosecaseController {
 public Case caseObj {get;set;}
    public Id tempCaseId {get;set;}    
    public String tempTo {get;set;}
    public String tempCategory {get;set;}
    public String tempRootCause {get;set;}
    public String tempGBOType {get;set;}    
    public String tempSubCategory {get;set;}       
    public String tempInternalComments {get;set;}             
    public Case caseObj1;
    public Case parentCase;
  

    
       public gboclosecaseController(ApexPages.StandardController controller){
        this.caseObj = (Case)controller.getRecord();
        caseObj.RecordTypeID = '012560000000DrN';
         tempCaseId  = ApexPages.currentPage().getParameters().get('previousId');
         tempTo  = ApexPages.currentPage().getParameters().get('To');
         tempCategory  = ApexPages.currentPage().getParameters().get('Category');
         tempRootCause  = ApexPages.currentPage().getParameters().get('RootCause');
         tempGBOType  = ApexPages.currentPage().getParameters().get('GBOType');    
         tempSubCategory  = ApexPages.currentPage().getParameters().get('SubCategory');  
         tempInternalComments= ApexPages.currentPage().getParameters().get('InternalComments'); 

          /* List<Case> childCases = [select id from Case where ParentId=:caseObj.id AND Status!='Closed'];
        if(childCases.size()>0){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'You cannot Close Parent Case when any one of the child case is opened. Please close all the open child cases ')); 
                        
                }  */
         
        caseObj1 = [Select Status,ParentId,Id,CaseNumber,To__c,GBO_Type__c,Description,Root_Cause__c from case where id =:caseObj.Id]; 
                          
         system.debug('tempCaseId------------------1'+tempCaseId);    
             

         
          if(tempTo!=null && tempTo!='')
            caseObj.To__c = tempTo;    
              
         if(tempCategory!=null && tempCategory!='')
            caseObj.Category__c = tempCategory;    
            
          if(tempRootCause!=null && tempRootCause!='')
            caseObj.Root_Cause__c = tempRootCause;  
                        
          if(tempGBOType!=null && tempGBOType!='')
            caseObj.GBO_Type__c = tempGBOType;  
            
          if(tempSubCategory!=null && tempSubCategory!='')
            caseObj.SubCategory__c = tempSubCategory;  
            
          if(tempInternalComments!=null && tempInternalComments!='')
            caseObj.Description = tempInternalComments;                         
 
    }
    
    
 public  PageReference NewSave()
    {
      PageReference pageRef = Null;
      
        List<Case> childCases = [select id from Case where ParentId=:caseObj.id AND Status!='Closed'];
        if(childCases.size()>0){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'You cannot Close Parent Case when any one of the child case is opened. Please close all the open child cases ')); 
                return null;        
                }  
               
         if (caseObj.To__c != tempTo) 

            { 
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'You cannot change the "To" value')); 
                        return null; 
                }  
        
            
      try
     {
     
             if(caseObj1.ParentId !=null)
            {
               // parentCase = new Case();
               parentCase = [select id,CaseNumber,Account.Name,Owner.Email,ParentId from Case where Id=:caseObj1.ParentId];
                parentCase.Id = caseObj1.ParentId;
                parentCase.Status = 'In progress';
                
            }
            
            system.debug('parentCase------------------1'+parentCase);
            system.debug('caseObj1------------------1'+caseObj1);
   
   update caseObj;
   system.debug('caseObj------------------000000000000000000000000000000000000000'+caseObj);
   
            if(caseObj1.ParentId !=null)
            {       
                update parentCase;
            }
            
             if(caseObj1.ParentId !=null)
            {
        
        //Send an Email

        EmailTemplate et = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Name = 'GBOCASE02'];
        system.debug('SendEmail');

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       
                
        string CaseNumber=caseObj1.CaseNumber==null?'':caseObj1.CaseNumber;
        string ParentCaseNumber=parentCase.CaseNumber==null?'':parentCase.CaseNumber;
        string Accnt=caseObj.Account.Name==null?'':caseObj.Account.Name;
        //string OwnerName=parentCase.Owner.Name==null?'':parentCase.Owner.Name;
        string OwnerName=parentCase.Owner.Email;
        
        system.debug('SendEmail'+OwnerName);
        string To=caseObj.To__c==null?'':caseObj.To__c;
        string GBOType=caseObj.GBO_Type__c==null?'':caseObj.GBO_Type__c;
        string Reason=caseObj.Reason==null?'':caseObj.Reason;
        string Descrptn=caseObj.Description==null?'':caseObj.Description;
        string RootCause=caseObj.Root_Cause__c==null?'':caseObj.Root_Cause__c;


        
        String subject = et.Subject;
        subject = subject.replace('{!Case.CaseNumber}', CaseNumber);
        subject = subject.replace('{!Case.Parent}', ParentCaseNumber);

    
        String htmlBody = et.HtmlValue;
        htmlBody = htmlBody.replace('{!Case.CaseNumber}', CaseNumber);
        htmlBody = htmlBody.replace('{!Case.Parent}', ParentCaseNumber);
        htmlBody = htmlBody.replace('{!Case.To__c}', To);
        htmlBody = htmlBody.replace('{!Case.GBO_Type__c}', GBOType);
        htmlBody = htmlBody.replace('{!Case.Reason}', Reason);
        htmlBody = htmlBody.replace('{!Case.Description}', Descrptn);
        htmlBody = htmlBody.replace('{!Case.Root_Cause__c}', RootCause);
       
        mail.setSenderDisplayName('RSA Notification');
        mail.setInReplyTo('noreply@rsa.com');
        mail.setSubject(subject);
        mail.setHtmlBody(htmlBody);
        mail.setToaddresses(new String[] {OwnerName});
        
        system.debug('SingleEmail.svp'+mail);
       // Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    } 
   
    
      
     } 
    
     
     catch(Exception e){
                ApexPages.addMessages(e);
                return null;
            }              
  
      
    
     pageRef = new PageReference('/apex/CaseView?id='+caseObj.Id);
     return pageRef;
    }
}


********************************************** here my test class  *********************

@isTest(SeeAllData=true)
public class gboclosecaseController_test{

        public static testMethod void gboclosecase(){
        case objCase=new Case(status='New',Origin='Phone');
        insert objCase;
       
 
                 
        case objCase1=new Case(status='New',Origin='Phone',ParentId=objCase.Id);
        EmailTemplate et = new EmailTemplate (Subject='Test',HtmlValue='gbocase',Body ='templete');
       
        //insert et;
        /*list<case> cn =new list<case>();
     
        cn.add(new case(status='New',Origin='Phone',ParentId=objCase.Id));
        insert cn;*/
       
               
               
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       
        ApexPages.currentPage().getParameters().put('caseId',objCase.Id);
        ApexPages.StandardController sc = new ApexPages.StandardController(objCase);
        gboclosecaseController GCC= new gboclosecaseController (sc);
       
       
         GCC.NewSave();
      
        
       
            }
}
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class and let us know if that will help u
@isTest(SeeAllData=true)
public class gboclosecaseController_test
{
        public static testMethod void gboclosecase()
		{
			case ParentCase=new Case(status='New',Origin='Phone');
			insert ParentCase;

			case objCase=new Case(status='New',Origin='Phone',ParentId=ParentCase.Id);
			insert objCase;
       
			ApexPages.currentPage().getParameters().put('previousId',objCase.Id);
			ApexPages.currentPage().getParameters().put('To','Test@Test.com');
			ApexPages.currentPage().getParameters().put('Category','Category');
			ApexPages.currentPage().getParameters().put('RootCause','RootCause');
			ApexPages.currentPage().getParameters().put('GBOType','GBOType');
			ApexPages.currentPage().getParameters().put('SubCategory','SubCategory');
			ApexPages.currentPage().getParameters().put('InternalComments','InternalComments');
			
			ApexPages.currentPage().getParameters().put('caseId',objCase.Id);
			ApexPages.StandardController sc = new ApexPages.StandardController(objCase);
			gboclosecaseController GCC= new gboclosecaseController (sc);
       
			try
			{
				GCC.NewSave();
			}Catch(Exception ee)
			{	}			
        }

        public static testMethod void gboParentclosecase()
		{
			case objCase=new Case(status='New',Origin='Phone');
			insert objCase;
       
			case objCase1=new Case(status='New',Origin='Phone',ParentId=objCase.Id);
			insert objCase1;

			EmailTemplate et = new EmailTemplate (Subject='Test',HtmlValue='gbocase',Body ='templete');
       
		   
			ApexPages.currentPage().getParameters().put('previousId',objCase.Id);
			ApexPages.currentPage().getParameters().put('To','Test@Test.com');
			ApexPages.currentPage().getParameters().put('Category','Category');
			ApexPages.currentPage().getParameters().put('RootCause','RootCause');
			ApexPages.currentPage().getParameters().put('GBOType','GBOType');
			ApexPages.currentPage().getParameters().put('SubCategory','SubCategory');
			ApexPages.currentPage().getParameters().put('InternalComments','InternalComments');
			
			ApexPages.currentPage().getParameters().put('caseId',objCase.Id);
			ApexPages.StandardController sc = new ApexPages.StandardController(objCase);
			gboclosecaseController GCC= new gboclosecaseController (sc);
       
       
			try
			{
				GCC.NewSave();
			}Catch(Exception ee)
			{	}			
        }

}

Let us know if this will help you

 
ChinnoduChinnodu
AmitThank u so very much.. But am not able to cover the one can u pls help me..
 EmailTemplate et = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Name = 'GBOCASE02'];
        system.debug('SendEmail');

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       
                
        string CaseNumber=caseObj1.CaseNumber==null?'':caseObj1.CaseNumber;
        string ParentCaseNumber=parentCase.CaseNumber==null?'':parentCase.CaseNumber;
        string Accnt=caseObj.Account.Name==null?'':caseObj.Account.Name;
        //string OwnerName=parentCase.Owner.Name==null?'':parentCase.Owner.Name;
        string OwnerName=parentCase.Owner.Email;
        
        system.debug('SendEmail'+OwnerName);
        string To=caseObj.To__c==null?'':caseObj.To__c;
        string GBOType=caseObj.GBO_Type__c==null?'':caseObj.GBO_Type__c;
        string Reason=caseObj.Reason==null?'':caseObj.Reason;
        string Descrptn=caseObj.Description==null?'':caseObj.Description;
        string RootCause=caseObj.Root_Cause__c==null?'':caseObj.Root_Cause__c;


        
        String subject = et.Subject;
        subject = subject.replace('{!Case.CaseNumber}', CaseNumber);
        subject = subject.replace('{!Case.Parent}', ParentCaseNumber);

    
        String htmlBody = et.HtmlValue;
        htmlBody = htmlBody.replace('{!Case.CaseNumber}', CaseNumber);
        htmlBody = htmlBody.replace('{!Case.Parent}', ParentCaseNumber);
        htmlBody = htmlBody.replace('{!Case.To__c}', To);
        htmlBody = htmlBody.replace('{!Case.GBO_Type__c}', GBOType);
        htmlBody = htmlBody.replace('{!Case.Reason}', Reason);
        htmlBody = htmlBody.replace('{!Case.Description}', Descrptn);
        htmlBody = htmlBody.replace('{!Case.Root_Cause__c}', RootCause);
       
        mail.setSenderDisplayName('RSA Notification');
        mail.setInReplyTo('noreply@rsa.com');
        mail.setSubject(subject);
        mail.setHtmlBody(htmlBody);
        mail.setToaddresses(new String[] {OwnerName});
        
        system.debug('SingleEmail.svp'+mail);
       // Messaging.sendEmail(new Me 
ChinnoduChinnodu
yes it is available in my org
ChinnoduChinnodu
Amit , after executing am not getting any error .
Amit Chaudhary 8Amit Chaudhary 8
Please update  your main class like below and the try above test class
NOTE:- Please debug your code.
 
public with sharing class gboclosecaseController 
{
	public Case caseObj {get;set;}
    public Id tempCaseId {get;set;}    
    public String tempTo {get;set;}
    public String tempCategory {get;set;}
    public String tempRootCause {get;set;}
    public String tempGBOType {get;set;}    
    public String tempSubCategory {get;set;}       
    public String tempInternalComments {get;set;}             
    public Case caseObj1;
    public Case parentCase;
  
    public gboclosecaseController(ApexPages.StandardController controller)
	{
			this.caseObj = (Case)controller.getRecord();
			caseObj.RecordTypeID = '012560000000DrN';
			tempCaseId  = ApexPages.currentPage().getParameters().get('previousId');
			 tempTo  = ApexPages.currentPage().getParameters().get('To');
			 tempCategory  = ApexPages.currentPage().getParameters().get('Category');
			 tempRootCause  = ApexPages.currentPage().getParameters().get('RootCause');
			 tempGBOType  = ApexPages.currentPage().getParameters().get('GBOType');    
			 tempSubCategory  = ApexPages.currentPage().getParameters().get('SubCategory');  
			 tempInternalComments= ApexPages.currentPage().getParameters().get('InternalComments'); 

          /* List<Case> childCases = [select id from Case where ParentId=:caseObj.id AND Status!='Closed'];
        if(childCases.size()>0){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'You cannot Close Parent Case when any one of the child case is opened. Please close all the open child cases ')); 
                        
                }  */
         
        caseObj1 = [Select Status,ParentId,Id,CaseNumber,To__c,GBO_Type__c,Description,Root_Cause__c from case where id =:caseObj.Id]; 
                          
         system.debug('tempCaseId------------------1'+tempCaseId);    
             

         
          if(tempTo!=null && tempTo!='')
            caseObj.To__c = tempTo;    
              
         if(tempCategory!=null && tempCategory!='')
            caseObj.Category__c = tempCategory;    
            
          if(tempRootCause!=null && tempRootCause!='')
            caseObj.Root_Cause__c = tempRootCause;  
                        
          if(tempGBOType!=null && tempGBOType!='')
            caseObj.GBO_Type__c = tempGBOType;  
            
          if(tempSubCategory!=null && tempSubCategory!='')
            caseObj.SubCategory__c = tempSubCategory;  
            
          if(tempInternalComments!=null && tempInternalComments!='')
            caseObj.Description = tempInternalComments;                         
 
    }
    
    
 public  PageReference NewSave()
    {
      PageReference pageRef = Null;
      
        List<Case> childCases = [select id from Case where ParentId=:caseObj.id AND Status!='Closed'];
        if(childCases.size()>0){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'You cannot Close Parent Case when any one of the child case is opened. Please close all the open child cases ')); 
                return null;        
                }  
               
         if (caseObj.To__c != tempTo) 

            { 
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'You cannot change the "To" value')); 
                        return null; 
                }  
        
            
      try
     {
     
            if(caseObj1.ParentId !=null)
            {
				parentCase = [select id,CaseNumber,Account.Name,Owner.Email,ParentId from Case where Id=:caseObj1.ParentId];
                //parentCase.Id = caseObj1.ParentId;
                parentCase.Status = 'In progress';
                
            }
            
   
			update caseObj;
   
            if(caseObj1.ParentId !=null)
            {       
                update parentCase;
            }
				System.debug('************************After Update*********************');
             if(caseObj1.ParentId !=null)
            {
        
        //Send an Email

        EmailTemplate et = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Name = 'GBOCASE02'];
        system.debug('SendEmail');

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       
                
        string CaseNumber=caseObj1.CaseNumber==null?'':caseObj1.CaseNumber;
        string ParentCaseNumber=parentCase.CaseNumber==null?'':parentCase.CaseNumber;
        string Accnt=caseObj.Account.Name==null?'':caseObj.Account.Name;
        //string OwnerName=parentCase.Owner.Name==null?'':parentCase.Owner.Name;
        string OwnerName=parentCase.Owner.Email;
        
        system.debug('SendEmail'+OwnerName);
        string To=caseObj.To__c==null?'':caseObj.To__c;
        string GBOType=caseObj.GBO_Type__c==null?'':caseObj.GBO_Type__c;
        string Reason=caseObj.Reason==null?'':caseObj.Reason;
        string Descrptn=caseObj.Description==null?'':caseObj.Description;
        string RootCause=caseObj.Root_Cause__c==null?'':caseObj.Root_Cause__c;


        
        String subject = et.Subject;
        subject = subject.replace('{!Case.CaseNumber}', CaseNumber);
        subject = subject.replace('{!Case.Parent}', ParentCaseNumber);

    
        String htmlBody = et.HtmlValue;
        htmlBody = htmlBody.replace('{!Case.CaseNumber}', CaseNumber);
        htmlBody = htmlBody.replace('{!Case.Parent}', ParentCaseNumber);
        htmlBody = htmlBody.replace('{!Case.To__c}', To);
        htmlBody = htmlBody.replace('{!Case.GBO_Type__c}', GBOType);
        htmlBody = htmlBody.replace('{!Case.Reason}', Reason);
        htmlBody = htmlBody.replace('{!Case.Description}', Descrptn);
        htmlBody = htmlBody.replace('{!Case.Root_Cause__c}', RootCause);
       
        mail.setSenderDisplayName('RSA Notification');
        mail.setInReplyTo('noreply@rsa.com');
        mail.setSubject(subject);
        mail.setHtmlBody(htmlBody);
        mail.setToaddresses(new String[] {OwnerName});
        
        system.debug('SingleEmail.svp'+mail);
       // Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    } 
   
    
      
     } 
    
     
     catch(Exception e){
	 				System.debug('************************Exception*********************'+e);

                ApexPages.addMessages(e);
                return null;
            }              
  
      
    
     pageRef = new PageReference('/apex/CaseView?id='+caseObj.Id);
     return pageRef;
    }
}

Let us know if this will help you

Thanks
Amit Chaudhary