3

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

0

2 Answers 2

9

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);

    }
}
1
  • Thank you very much actually problem was , I was Inserting another object Record. I rectified it
    – Sharat
    Commented Dec 22, 2015 at 6:37
2

Your problem is with these lines:

parentId = ApexPages.currentPage().getParameters().get('Id');
Rec = [SELECT id,Contact_Email__c FROM Payment_Receipts__c WHERE Id =: parentId ];

Because in your test, you do not set that parameter before calling the constructor. As your extension is written, you need to set that parameter before constructing it. A more reasonable approach would be to just use the StandardController method getId, which is the way the class is intended to be used.

parentId = controller.getId();
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .