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
llisallisa 

Hello Everyone i am stuck on the test class for Emailbody...please someone help me on this.

public class KEmailBodyController{
    public K_Approval_Request_Body__c body{get;set;}
    String kId = '';
    public KEmailBodyController(){
        body = new K_Approval_Request_Body__c();
        kId = ApexPages.currentPage().getParameters().get('id');
        if(common.NVL(kId) != ''){
            List<K_Approval_Request_Body__c> lstBody = [Select Id, Body__c, Related_Document__c, Status__c
                From K_Approval_Request_Body__c Where Related_Document__c=:kId and Status__c = 'Pending' Limit 1];
            if(lstBody != null && lstBody.size()>0){
                body = lstBody[0];


            }
            else{
                body.Related_Document__c = kId;
                body.Status__c = 'Pending';
            }
        }
    }
    public void save(){
        if(body != null && common.NVL(body.Body__c) != ''){
            upsert body;
        }
    }
}

Plese help me,i have totally no idea about test classes.
Best Answer chosen by llisa
Amit Chaudhary 8Amit Chaudhary 8
Please try below class.
public class KEmailBodyController
{
    public K_Approval_Request_Body__c body{get;set;}
    String kId = '';
    public KEmailBodyController()
	{
        body = new K_Approval_Request_Body__c();
        kId = ApexPages.currentPage().getParameters().get('id');
        if(kId!= null && kId != '')
		{
            List<K_Approval_Request_Body__c> lstBody = [Select Id, Body__c, Related_Document__c, Status__c
                From K_Approval_Request_Body__c Where Related_Document__c=:kId and Status__c = 'Pending' Limit 1];
            if(lstBody != null && lstBody.size()>0){
                body = lstBody[0];


            }
            else{
                body.Related_Document__c = kId;
                body.Status__c = 'Pending';
            }
        }
    }
    public void save()
	{
        if(body != null && body.Body__c != '' )
		{
            upsert body;
        }
    }
}


Please try below Test Class
@isTest
private class test_KEmailBodyControllerTest 
{
    static testMethod void test_KAddNoteControllers1()
    {
		K_Document__c k1=new K_Document__c();
		k1.note__c='abcde';
		k1.Order_Number__c='123';
		insert k1;
		
        K_Approval_Request_Body__c kApproval = new K_Approval_Request_Body__c();
        kApproval.Body__c ='TEST1';
        kApproval.Status__c ='Pending';
        kApproval.Related_Document__c =k1.id;
        insert kApproval;    
         
        PageReference pageRef = Page.KEmailBody; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(k1.id));
        Test.setCurrentPage(pageRef);
  
        KEmailBodyController controller = new KEmailBodyController();
        controller.save();
			
    }

    static testMethod void test_KAddNoteControllers2()
    {
		K_Document__c k1=new K_Document__c();
		k1.note__c='abcde';
		k1.Order_Number__c='123';
		insert k1;
		
        K_Approval_Request_Body__c kApproval = new K_Approval_Request_Body__c();
        kApproval.Body__c ='TEST1';
        kApproval.Status__c ='Pending';
		kApproval.Related_Document__c =k1.id;
        insert kApproval;    
         
        PageReference pageRef = Page.KEmailBody; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(k1.id));
        Test.setCurrentPage(pageRef);
  
        KEmailBodyController controller = new KEmailBodyController();
        controller.save();
			
    }

}


 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
@isTest
private class KEmailBodyControllerTest 
{
    boolean displayPopup = true;
    static testMethod void test_KAddNoteControllers1()
	{
		K_Approval_Request_Body__c kApproval = new K_Approval_Request_Body__c();
		//kApproval.Field =value; Please add all required field values
		insert kApproval;	
		 
		PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
		pageRef.getParameters().put('id', String.valueOf(kApproval.id));
		Test.setCurrentPage(pageRef);
  
        KEmailBodyController controller = new KEmailBodyController();
		controller.save();
    }
	

}
Pleae check below blog for test class i hope that will help you
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
http://amitsalesforce.blogspot.in/2015/06/salesforce-testing-best-practice.html
http://amitsalesforce.blogspot.in/2015/05/testsetup-set-up-test-data-for-entire.html
http://amitsalesforce.blogspot.in/2015/02/starttest-and-stoptest-method.html

Please let us know if this will help you

Thanks
Amit Chaudhary
llisallisa
Hi amit,
thanks for your help,but it is not working for "KEmailBodyController".
Amit Chaudhary 8Amit Chaudhary 8
Please let me know what issue you are facing with code coverage
llisallisa
Hello Amit,
it is showing 0% code coverage.ang the "boolean displayPopup = true" ang pagrreference thing is not there in "KEmailBodyController".CLASS also.
Amit Chaudhary 8Amit Chaudhary 8
Please share your test class and main class
llisallisa

my class is --

public class KEmailBodyController{
    public K_Approval_Request_Body__c body{get;set;}
    String kId = '';
    public KEmailBodyController(){
        body = new K_Approval_Request_Body__c();
        kId = ApexPages.currentPage().getParameters().get('id');
        if(common.NVL(kId) != ''){
            List<K_Approval_Request_Body__c> lstBody = [Select Id, Body__c, Related_Document__c, Status__c
                From K_Approval_Request_Body__c Where Related_Document__c=:kId and Status__c = 'Pending' Limit 1];
            if(lstBody != null && lstBody.size()>0){
                body = lstBody[0];


            }
            else{
                body.Related_Document__c = kId;
                body.Status__c = 'Pending';
            }
        }
    }
    public void save(){
        if(body != null && common.NVL(body.Body__c) != ''){
            upsert body;
        }
    }
}

and test class is---

@isTest
private class test_KEmailBodyControllerTest 
{
    static testMethod void test_KAddNoteControllers1()
    {
        K_Approval_Request_Body__c kApproval = new K_Approval_Request_Body__c();
        kApproval.Body__c ='TEST1';
        kApproval.Related_Document__c ='TEST2';
        kApproval.Status__c ='TEST3';
        insert kApproval;    
         
        PageReference pageRef = Page.KEmailBody; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(kApproval.id));
        Test.setCurrentPage(pageRef);
  
        KEmailBodyController controller = new KEmailBodyController();
        controller.save();
    }
    

}

Hey AMIT can we communicate throught mail?
Amit Chaudhary 8Amit Chaudhary 8
Please update your class like below
public class KEmailBodyController
{
    public K_Approval_Request_Body__c body{get;set;}
    String kId = '';
    public KEmailBodyController()
	{
        body = new K_Approval_Request_Body__c();
        kId = ApexPages.currentPage().getParameters().get('id');
        if(kId!= null && kId != '')
		{
            List<K_Approval_Request_Body__c> lstBody = [Select Id, Body__c, Related_Document__c, Status__c
                From K_Approval_Request_Body__c Where Related_Document__c=:kId and Status__c = 'Pending' Limit 1];
            if(lstBody != null && lstBody.size()>0){
                body = lstBody[0];


            }
            else{
                body.Related_Document__c = kId;
                body.Status__c = 'Pending';
            }
        }
    }
    public void save()
	{
        if(body != null && body.Body__c != '' )
		{
            upsert body;
        }
    }
}
@isTest
private class test_KEmailBodyControllerTest 
{
    static testMethod void test_KAddNoteControllers1()
    {
        K_Approval_Request_Body__c kApproval = new K_Approval_Request_Body__c();
        kApproval.Body__c ='TEST1';
        kApproval.Status__c ='Pending';
        insert kApproval;    
         
        PageReference pageRef = Page.KEmailBody; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(kApproval.id));
        Test.setCurrentPage(pageRef);
  
        KEmailBodyController controller = new KEmailBodyController();
        controller.save();
    }

}


 
llisallisa
Hi Amit,
             I did canges on my apex class but it dont work,0% code coverage is showing.
Abhishek BansalAbhishek Bansal
Hi,

Please try the below test class :
 
@isTest
private class test_KEmailBodyControllerTest 
{
    static testMethod void test_KAddNoteControllers1()
    {
		Related_Document__c testRelatedDoc = new Related_Document__c(Name='Test');//Replace the name from your custom object name and fill all required fields
		insert testRelatedDoc;
        
		K_Approval_Request_Body__c kApproval = new K_Approval_Request_Body__c();
        kApproval.Body__c ='TEST1';
		kApproval.Related_Document__c = testRelatedDoc.id;
        kApproval.Status__c ='Pending';
        insert kApproval;    
         
        PageReference pageRef = Page.KEmailBody; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(testRelatedDoc.id));
        Test.setCurrentPage(pageRef);
		
		Test.startTest();
        KEmailBodyController controller = new KEmailBodyController();
        controller.save();
		Test.stopTest();
    }
	
	static testMethod void test_KAddNoteControllers2()
    {
		Related_Document__c testRelatedDoc = new Related_Document__c(Name='Test');//Replace the name from your custom object name and fill all required fields
		insert testRelatedDoc;
        
		K_Approval_Request_Body__c kApproval = new K_Approval_Request_Body__c();
        kApproval.Body__c ='TEST1';
        kApproval.Status__c ='Pending';
        insert kApproval;    
         
        PageReference pageRef = Page.KEmailBody; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(testRelatedDoc.id));
        Test.setCurrentPage(pageRef);
		
		Test.startTest();
        KEmailBodyController controller = new KEmailBodyController();
        controller.save();
		Test.stopTest();
    }
}
Regards,
Abhishek.
 
Amit Chaudhary 8Amit Chaudhary 8
Please click on Run Test Button on top of your test class
Abhishek BansalAbhishek Bansal
Monalisa, Each and everytime when you update a test class than you have to run it again to see the code coverage.
You will not see any changes in Code Coverage unless you run it agian.
So make sure that you are running your test class each time after you are doing changes in it.
llisallisa
HI,
   Each time when i update ,i run the test class.
Here "Related_Document__c" having master detail relationship with another object.
so in this code it gives an error "Invalid Type-Related_Document__c"
Amit Chaudhary 8Amit Chaudhary 8
Please try below class.
public class KEmailBodyController
{
    public K_Approval_Request_Body__c body{get;set;}
    String kId = '';
    public KEmailBodyController()
	{
        body = new K_Approval_Request_Body__c();
        kId = ApexPages.currentPage().getParameters().get('id');
        if(kId!= null && kId != '')
		{
            List<K_Approval_Request_Body__c> lstBody = [Select Id, Body__c, Related_Document__c, Status__c
                From K_Approval_Request_Body__c Where Related_Document__c=:kId and Status__c = 'Pending' Limit 1];
            if(lstBody != null && lstBody.size()>0){
                body = lstBody[0];


            }
            else{
                body.Related_Document__c = kId;
                body.Status__c = 'Pending';
            }
        }
    }
    public void save()
	{
        if(body != null && body.Body__c != '' )
		{
            upsert body;
        }
    }
}


Please try below Test Class
@isTest
private class test_KEmailBodyControllerTest 
{
    static testMethod void test_KAddNoteControllers1()
    {
		K_Document__c k1=new K_Document__c();
		k1.note__c='abcde';
		k1.Order_Number__c='123';
		insert k1;
		
        K_Approval_Request_Body__c kApproval = new K_Approval_Request_Body__c();
        kApproval.Body__c ='TEST1';
        kApproval.Status__c ='Pending';
        kApproval.Related_Document__c =k1.id;
        insert kApproval;    
         
        PageReference pageRef = Page.KEmailBody; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(k1.id));
        Test.setCurrentPage(pageRef);
  
        KEmailBodyController controller = new KEmailBodyController();
        controller.save();
			
    }

    static testMethod void test_KAddNoteControllers2()
    {
		K_Document__c k1=new K_Document__c();
		k1.note__c='abcde';
		k1.Order_Number__c='123';
		insert k1;
		
        K_Approval_Request_Body__c kApproval = new K_Approval_Request_Body__c();
        kApproval.Body__c ='TEST1';
        kApproval.Status__c ='Pending';
		kApproval.Related_Document__c =k1.id;
        insert kApproval;    
         
        PageReference pageRef = Page.KEmailBody; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(k1.id));
        Test.setCurrentPage(pageRef);
  
        KEmailBodyController controller = new KEmailBodyController();
        controller.save();
			
    }

}


 
This was selected as the best answer
llisallisa
Thanks a lot Abhishek and Amit.This code is working succesfully.