How to Write Test Class for Standard Controller Along With Extensions

  • Hi Everyone I'm finding it difficult to write test class for this class

    Class

    public class SendEmailReceipt{
    
        public Payment_Receipts__c Rec;
    
        public string em{get;set;}
    
        public id parentId; 
        public SendEmailReceipt(ApexPages.StandardController controller) {
              parentId = ApexPages.currentPage().getParameters().get('Id');
              Rec = [SELECT id,Contact_Email__c FROM Payment_Receipts__c WHERE Id =: parentId ];
              em = rec.Contact_Email__c;
          System.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+ Rec );
        }
    
        public string cc{get;set;}
    
    
       Public List<Payment_Receipts__c>rec9;
    
    
        public PageReference sendEmail()
    
            {
    
          //Email Body content here
               return null;
            }
    }
    

    Test Class

    @istest(SeeAlldata=True)
    private class testsendemailreciept
    {
        private static testmethod void SendEmailCustomerDetailPdf1()
         {
    
    
         List<Invoice__c> inv1=new list<Invoice__c>();
         List<Integer> i =new List<Integer>();      
    
    
         Contact con= new contact();
         con.Email = '[email protected]';
         con.LastName= 'ds';
         con.MobilePhone= '9898561236';
         insert con;
    
    
         Opportunities__c pon=new Opportunities__c();
         pon.name='Tesr';
         pon.Contact__c= con.id;            
         insert pon;         
    
         Payment_receipt__c pay =new Payment_receipt__c();
         pay.Opportunity__c=pon.id; 
         insert pay;           
    
         ApexPages.StandardController sc = new ApexPages.StandardController(pay);
         SendEmailReceipt testAccPlan = new SendEmailReceipt(sc);
    
         PageReference pageRef = Page.SendEmailReceipt1;
         pageRef.getParameters().put('Id', String.valueOf(pay.Id));
         Test.setCurrentPage(pageRef);  
    
             }
         }
    

    Error Which I'm Facing

    System.QueryException: List has no rows for assignment to SObject Stack Trace Class.SendEmailReceipt.: line 10, column 1 Class.testsendemailreciept.SendEmailCustomerDetailPdf1: line 29, column 1

  • Ratan Paul

    Ratan Paul Correct answer

    6 years ago

    When you use ApexPages.currentPage().getParameters().get('Id');

    You need to set Test.setCurrentPage(pageRef); and pageRef.getParameters().put('Id', String.valueOf(obj.Id));

    PageReference pageRef = Page.SendEmailReceipt1;
    Test.setCurrentPage(pageRef);
    pageRef.getParameters().put('Id', String.valueOf(obj.Id));
    ApexPages.StandardController sc = new ApexPages.StandardController(obj);
    SendEmailReceipt testAccPlan = new SendEmailReceipt(sc);
    

    example

    public class SendEmailReceipt 
    {
    
        public Account Rec;
    
        public id parentId; 
        public SendEmailReceipt(ApexPages.StandardController controller) {
              parentId = ApexPages.currentPage().getParameters().get('Id');
              Rec = [SELECT id FROM Account WHERE Id =: parentId ];
          System.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+ Rec );
        }
    }
    

    Test Class

    @istest()
    public class testsendemailreciept
    {
          private static testmethod void SendEmailCustomerDetailPdf1()
         {
    
    
         Account obj = new Account(Name ='Test');
         insert obj;
    
             PageReference pageRef = Page.SendEmailReceipt1;
             Test.setCurrentPage(pageRef);
    
         pageRef.getParameters().put('Id', String.valueOf(obj.Id));
         ApexPages.StandardController sc = new ApexPages.StandardController(obj);
         SendEmailReceipt testAccPlan = new SendEmailReceipt(sc);
    
             }
         }
    

    It will solve your problem.

    Or

    When we use StandardController no need to use ApexPages.currentPage().getParameters().get('Id') instead just use controller.getId();

    Example

    public class SendEmailReceipt 
    {
    
        public Account Rec;
    
        public id parentId; 
        public SendEmailReceipt(ApexPages.StandardController controller) {
              parentId = controller.getId();
              Rec = [SELECT id FROM Account WHERE Id =: parentId ];
          System.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+ Rec );
        }
    }
    

    Test Class

    @istest()
    public class testsendemailreciept
    {
         private static testmethod void SendEmailCustomerDetailPdf1()
         {
    
    
             Account obj = new Account(Name ='Test');
             insert obj;
    
             PageReference pageRef = Page.SendEmailReceipt1;
             Test.setCurrentPage(pageRef);
             ApexPages.StandardController sc = new ApexPages.StandardController(obj);
             SendEmailReceipt testAccPlan = new SendEmailReceipt(sc);
    
        }
    }
    

    Thank you very much actually problem was , I was Inserting another object Record. I rectified it

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM