0

This is my first test class for Apex Controller. Here is my whole scenario 1. I have a VF page which loads Quotes in a list which can be selected 2. On this page, user can select the quotes and click a button to generate a PDF page which is different VF page. the functionality works perfectly fine. Now I am writing the test class and able to pass through Constructor function. But not sure how to write the test class for Display function. I have added all three controllers here which are the part of both of the VF page and test class written so far

Class list Quotes and provides option for selection

    public class MyQuoteListCntrlr{
         // PROPERTIES
         public List<QuoteWrapperCls> quoteList {get;set;}
         public List<QQuoteWrapperCls > QquoteList{get;set;}

         public List<QuoteIncludeWP> QuoteIncludes{get;set;}
         public Set<String> selQuoteAppId{get;set;}
         public Set<String> selQuoteDecId{get;set;}
         public string HTMLString {get;set;}
         public Boolean hasSelQuote {get;set;}

         Date myDate = system.today();

         public Opportunity oppData {get; set;}
         public Opportunity oppPData {get; set;}

            // PROPERTIES

         public Quote_Application__c qquoteData {get; set;}
         public LIST<Quote> AppQuote {get;set;}
         public LIST<Quote> DecQuote {get;set;}

        public Datetime ToDayDate {get; set;}
        public  String dateOutput {get; set;}


         // CONSTRUCTOR
         public MyQuoteListCntrlr(ApexPages.StandardController controller){




                Id recordId = controller.getId();



                oppData =[Select Id,Name, account.name, Owner.Name, Physical_Address_Street__c, Physical_Address_City__c, Physical_Address_State__c, Physical_Address_Zip__c, First_Name__c, Last_Name__C from Opportunity where id=:recordId ];



              quoteList = new List<QuoteWrapperCls>();



              for(Quote a : [SELECT ID, QuoteNumber, Status,Carrier__c, Account__c,DownPayment__c,Down_Payment_Taken__c, MonthlyPayments__c, Premium_Amount__c
              FROM Quote where status in ('Carrier Approved','Carrier Declined')  and opportunityid =: oppData.Id
              ]){
                   quoteList.add(new QuoteWrapperCls(a));
              }

              QquoteList= new List<QQuoteWrapperCls >(); 


              for(Quote_Application__c QQ : [SELECT ID,Name, Dot__c,Docket__c, Email__c FROM Quote_Application__c where OpportunityQuickQuote__c=: oppData.Id order by LastModifiedDate LIMIT 1
              ]){
                   QquoteList.add(new QQuoteWrapperCls (QQ ));
              }

         }


                 public PageReference Display()

                 {
                    String ProposalIdAppr;
                    String ProposalIdDec;
                    selQuoteAppId= new Set<String>();
                    selQuoteAppId.clear();

                    selQuoteDecId= new Set<String>();
                    selQuoteDecId.clear();
                    Integer Count;
                    Count=0;

                     ProposalIdAppr= Datetime.now().format('mm-dd-yyyy HH:mm:ss.SSS');
                     for(QuoteWrapperCls cWrapper : quoteList){
                     if(cWrapper.isSelected && cWrapper.cQuote.Status=='Carrier Approved'){

                        ProposalQuotes__c ProposalQuote = new ProposalQuotes__c(); 
                        ProposalQuote.ProposalID__c =  ProposalIdAppr;  
                        ProposalQuote.QuoteID__c    =cWrapper.cQuote.Id  ;

                        insert ProposalQuote ;

                        Count = count +1;


                   }}

                    ToDayDate = Datetime.now();
                    ProposalIdDec = Datetime.now().format('mm-dd-yyyy HH:mm:ss.SSS');
              for(QuoteWrapperCls cWrapperDec : quoteList){
                   if(cWrapperDec.isSelected && cWrapperDec.cQuote.Status=='Carrier Declined'){

                        ProposalQuotes__c ProposalQuote = new ProposalQuotes__c(); 
                        ProposalQuote.ProposalID__c =  ProposalIdDec;  
                        ProposalQuote.QuoteID__c    =cWrapperDec.cQuote.Id  ;

                        insert ProposalQuote ;
                         Count = count +1;
                   }}
                   If (Count ==0)
                   {
                       Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,''+'No Quote has been selected for proposal"'));
                       return page.proposalform;
                   }  


                        PageReference pdf = new PageReference('/apex/proposal?Id=' + oppData.Id + '&ProposalIdAppr=' + ProposalIdAppr + '&ProposalIdDec=' + ProposalIdDec); 


                              pdf.setredirect(true);
                            // need to pass unit test -- current bug   








                      return pdf;

                             }

            }


public class QQuoteWrapperCls {
     public Boolean isSelected {get;set;}
     public Quote_Application__c cQQuote {get;set;}

     public QQuoteWrapperCls(Quote_Application__c cQQuote ){
          this.cQQuote = cQQuote ;
     }
}


public class MyProposalCntrlr{
// PROPERTIES
     public List<QuoteWrapperCls> quoteList {get;set;}
     public List<QQuoteWrapperCls > QquoteList{get;set;}

     public List<QuoteIncludeWP> QuoteIncludes{get;set;}
     public Set<String> selQuoteAppId{get;set;}
     public Set<String> selQuoteDecId{get;set;}
     public string HTMLString {get;set;}
     public Boolean hasSelQuote {get;set;}
     public String StrId{get;set;}

     Date myDate = system.today();


     public Opportunity oppData {get; set;}

        // PROPERTIES

     public Quote_Application__c qquoteData {get; set;}
     public LIST<Quote> AppQuote {get;set;}
     public LIST<Quote> DecQuote {get;set;}

    public Datetime ToDayDate {get; set;}
    public  String dateOutput {get; set;}
     public LIST<Commodities_transported__c> ctData {get; set;}
     public LIST<Driver__c> DriverData {get; set;}
     public LIST<Garage__c> GarageData {get; set;}
     public LIST<Vehicle__c> VehicleData {get; set;}
     public String selQuoteSelected {get; set;}


     // CONSTRUCTOR
     public MyProposalCntrlr(ApexPages.StandardController controller){
           selQuoteAppId= new Set<String>();
            selQuoteAppId.clear();

            selQuoteDecId= new Set<String>();
            selQuoteDecId.clear();


            Id recordId = controller.getId();


             controller.addFields(new String[]{

                'Id', 'Name', 'account.name', 'Owner.Name', 'Owner.Email', 'Owner.Extension', 'Physical_Address_Street__c', 'Physical_Address_City__c', 'Physical_Address_State__c', 'Physical_Address_Zip__c', 'First_Name__c', 'Last_Name__C'

            });
            oppData = (Opportunity)controller.getRecord();


            LIST<ProposalQuotes__c> ProposalApprQuote ;

            ProposalApprQuote = [select QuoteID__c   From ProposalQuotes__c where ProposalID__c =:apexpages.currentpage().getparameters().get('ProposalIdAppr')];

            for(ProposalQuotes__c cWrapper : ProposalApprQuote ){


                    selQuoteAppId.add( cWrapper.QuoteID__c);


                }


              LIST<ProposalQuotes__c> ProposalDecQuote ;
                 ProposalDecQuote = [select QuoteID__c  From ProposalQuotes__c where ProposalID__c =:apexpages.currentpage().getparameters().get('ProposalIdDec')];

                for(ProposalQuotes__c cWrapperDec : ProposalDecQuote ){


                    selQuoteDecId.add( cWrapperDec.QuoteID__c);


                }

           ToDayDate = Datetime.now();
           dateOutput = ToDayDate.format('MM/dd/yyyy');

           AppQuote  = [Select id, name, carrier__r.name,    Effective_Date_of_Policy__c,  Of_Payments__c,Auto_Liability__c,BOP__c,General_Liability__c,Cargo__c,CPP__c,DownPayment__c, MonthlyPayments__c,Physical_Damage__c, PL_Auto__c,PL_Home__c,PL_Umbrella__c,Premium_Amount__c,Transporter_Plate__c,Umbrella__c,Workers_Compensation__c,Garage_Keepers__c from Quote where id in:selQuoteAppId];
                HTMLString  ='';
           for (Quote Q : AppQuote  )
           {
            String EffectiveDate ='';
            IF(Q.Effective_Date_of_Policy__c <> null)
            {
               ToDayDate =Q.Effective_Date_of_Policy__c;

                EffectiveDate =  ToDayDate.format('MM/dd/yyyy');
            }
            HTMLString =HTMLString + '   <div style="width:700px; font-size:14px; padding-top:25px;  border-bottom:1px solid #000;font-weight:bold">Quote - '+  q.Carrier__r.name  + '</div> ';
            HTMLString =HTMLString + '<table><tr > <td Colspan ="8"></td></tr><tr><td style="color:#0b0b3b; font-size:12px; width:800px; padding-bottom:1px; padding-top:5px;" Colspan ="8"><b>Premium/Payment Information:</b></td>';
            HTMLString =HTMLString + '</tr><tr><td style="color:#0b0b3b; font-size:12px; padding-top:10px;"><b>Premium Amount: </b></td> <td style="color:#0b0b3b; font-size:12px;  ; padding-top:10px;"> $' + q.Premium_Amount__c  + '</td>' ;
            HTMLString =HTMLString  + '<td style="color:#0b0b3b; font-size:12px; padding-top:10px;" ><b>Monthly Payment: </b></td>';
            HTMLString =HTMLString  + '   <td style="color:#0b0b3b; font-size:12px; padding-top:10px;"> $' + q.MonthlyPayments__c  + '</td>';

             HTMLString =HTMLString  + '   <td style="color:#0b0b3b; font-size:12px; padding-top:10px;"><b>Down Payment: </b></td>';
             HTMLString =HTMLString  + '   <td style="color:#0b0b3b; font-size:12px;  padding-top:10px;"> $'+ Q.DownPayment__c +'</td>';
              HTMLString =HTMLString  + '   <td style="color:#0b0b3b; font-size:12px; padding-top:10px;" ><b># of Payment: </b></td>';
              HTMLString =HTMLString  + '  <td style="color:#0b0b3b; font-size:12px; padding-top:10px;">'+ Q.Of_Payments__c + '</td> </tr>';
              HTMLString =HTMLString + '<tr > <td Colspan ="8"></td></tr><tr><td style="color:#0b0b3b; font-size:12px; width:800px; padding-bottom:1px; padding-top:5px;" Colspan ="8"><b>Effective Date Of Policy: ' +  EffectiveDate + '</b></td>';
              HTMLString =HTMLString + '<tr > <td Colspan ="8"></td></tr><tr><td style="color:#0b0b3b; font-size:12px; width:800px; padding-bottom:1px; padding-top:5px;" Colspan ="8"><b>Quote Includes:</b></td>';
               QuoteIncludes= new List<QuoteIncludeWP>();
              if (Q.Auto_Liability__c ==true)
              {
                  QuoteIncludes.add(new QuoteIncludeWP('Auto Liability:',Q.Auto_Liability__c));  
                  }
              if (Q.BOP__c ==true)
              {
              QuoteIncludes.add(new QuoteIncludeWP('BOP:',Q.BOP__c));
              }
              if (Q.Cargo__c ==true)
              {
                  QuoteIncludes.add(new QuoteIncludeWP('Cargo:',Q.Cargo__c));
              }
              if (Q.CPP__c ==true)
              {
                  QuoteIncludes.add(new QuoteIncludeWP('CPP:',Q.CPP__c)); 
              }
              if (Q.Physical_Damage__c ==true)
              {
                  QuoteIncludes.add(new QuoteIncludeWP('Physical Damage:',Q.Physical_Damage__c)); 
              }
              if (Q.Auto_Liability__c ==true)
              {
                  QuoteIncludes.add(new QuoteIncludeWP('Garage Keepers:',Q.Auto_Liability__c)); 
              }
              if (Q.Umbrella__c ==true)
              {
                  QuoteIncludes.add(new QuoteIncludeWP('Umbrella:',Q.Umbrella__c)); 
              }
              if (Q.Transporter_Plate__c ==true)
              {
                  QuoteIncludes.add(new QuoteIncludeWP('Transporter Plate:',Q.Transporter_Plate__c)); 
              }
               if (Q.General_Liability__c ==true)
              {
                  QuoteIncludes.add(new QuoteIncludeWP('General Liability:',Q.General_Liability__c)); 
              }
           if (Q.PL_Auto__c ==true)
              { 
                  QuoteIncludes.add(new QuoteIncludeWP('PL - Auto:',Q.PL_Auto__c)); 
              }
            if (Q.Workers_Compensation__c ==true)
              { 
              QuoteIncludes.add(new QuoteIncludeWP('Workers Compensation:',Q.Workers_Compensation__c)); 
              }
            if (Q.PL_Home__c ==true)
              { 
              QuoteIncludes.add(new QuoteIncludeWP('PL - Home:',Q.PL_Home__c)); 
              }
           if (Q.PL_Umbrella__c ==true)
              { QuoteIncludes.add(new QuoteIncludeWP('PL - Umbrella:',Q.PL_Umbrella__c));
              }


              integer ColCount=0;
               Integer remainder ;
              for (QuoteIncludeWP QI : QuoteIncludes)

              {

                ColCount = ColCount +1;
                remainder = math.mod(ColCount, 4);
                  if (remainder  ==1)
                   {
                       HTMLString = HTMLString + '<tr>';
                  }
                   HTMLString = HTMLString + '<td style="color:#0b0b3b; font-size:12px; padding-top:10px;"><b>' + QI.lblName + '  </b></td> <td style="color:#0b0b3b; font-size:12px;  ; padding-top:10px;">' + + QI.lblValue+  + '</td>' ;

                   if (remainder  ==0)
                   {                       HTMLString = HTMLString + '</tr>';
                   }
              }
                       HTMLString = HTMLString + '</tr>   </table> ';
           }

          DecQuote = [Select id, name, carrier__r.name,   DeclinedReason__c from Quote where status ='Carrier Declined' and id in:selQuoteDecId]; 

        // oppData = [Select Id,Name, account.name, Owner.Name, Physical_Address_Street__c, Physical_Address_City__c, Physical_Address_State__c, Physical_Address_Zip__c, First_Name__c, Last_Name__C from Opportunity where id =:oppid ];




     qquotedata =   [SELECT Id, 
                     Name, Leads__c, 
                     Contacct_Agent__c, 
                     Agency__c, 
                     Email__c, 
                     PercentageSum__c,
                     QuoteApplicationDate__c, 
                     InsuredBusinessName__c, 
                     Street__c, 
                     City__c, 
                     Zip__c, 
                     State__c, 
                     InsuredPhoneNo__c, 
                    InsuredEmail__c, 
                     PrincipleOwnerName__c, 
                     Mailingaddress__c, 
                     MailingCity__c, 
                     Mailing_State__c, 
                     MailingZip__c, 
                     BusinessCell__c, 
                     BusinessFax__c, 
                     AlternateEmail__c, 
                     EIN__c, 
                     DOB__c,
                     OwnerId ,
                     CTCount__c, 
                     DriverCount__c, 
                     GarageCount__c, 
                     VehicleCount__c,
                     Social_Security__c, Cargo_Limit__c, Cargo_Deductable__c, Date_obtained_CDL__c,Drivers_License__c,  Drivers_License_State__c,
                     UM__c, UIM__c, PIP__c, Refrigeretor_Breakdown__c, Trailer_Interchange__c, Do_you_have_copy_of_contract__c, 
                     Trailer_Interchange_Limit__c, Non_owned_Trailer__c, NonOwnedTrailerLimit__c, CGL__c, UMB__c, WorkerCompensation__c, 
                     EstimatedPayrolExposure__c, CGLOccuranceLimit__c, CGLAggreegate__c, UMBOccuranceLimit__c, UMBAggregateLimit__c, CarrierOffering__c, 
                     CurrentPremium__c, Target_Premium__c, Carrier_Operation__c, Comments__c, OpportunityQuickQuote__c, OperationClassification__c, UM_PD__c, 
                     MedicalPayment__c, Docket__c, DOT__c, Liability__c, BusinessStartedYear__c ,No_Email__c
                     FROM Quote_Application__c where OpportunityQuickQuote__c =:oppData.Id order by LastModifiedDate LIMIT 1];
                      CTData =[SELECT Cargo_Info__c, Freight_percent__c,    Max_Value__c, Avg_Value__c FROM Commodities_transported__c WHERE Quote_Application__c =:qquotedata.ID];

                      DriverData =[SELECT Name_del__c, CDL_Year__c, Accidents__c, DateOfHire__c, DOB__c, License__c, State__c, Type__c, Violations__c from Driver__c WHERE Quote_Application__c =:qquotedata.ID];

                      GarageData =[SELECT City__c, Street__c,   Zip__c, State__c, Garage__c.Name  FROM Garage__c WHERE QuoteApplication__c =:qquotedata.ID];

                      VehicleData =[SELECT Name, CurrentValue__c, Deductable__c,    Garage__c,  GVWR__c, Make_Model__c, Radius__c, Safety_Device__c, Type__c, VIN__c, Year__c FROM vehicle__c WHERE Quote_Application__c =:qquotedata.ID]; 






         } 
               public pagereference saveAttach()

        {

                 //system.debug('The url created is' + pdf);

                 String ProposalIdAppr =apexpages.currentpage().getparameters().get('ProposalIdAppr');
                 String ProposalIdDec =apexpages.currentpage().getparameters().get('ProposalIdDec');
                    // create the new attachment */
                   Attachment attach = new Attachment(); 
                    Blob body;

                   PageReference pdf = new PageReference('/apex/proposalclone?Id=' + oppData.Id + '&ProposalIdAppr=' + ProposalIdAppr + '&ProposalIdDec=' + ProposalIdDec); 

                     // returns the output of the page as a PDFclone

                        system.debug('body should be fine');
                         pdf.setredirect(true);
                        // need to pass unit test -- current bug   
                         try { 
         body = pdf.getContentAsPDF(); 
      } catch (VisualforceException e) {
        body = Blob.valueOf('Some Text');
    }
        attach.Body = body;
        // add the user entered name
        attach.Name = oppData.Name + ' ' + myDate + '.pdf';
        attach.IsPrivate = false;
         //attach the pdf to the account
        attach.ParentId = oppData.Id;
        insert attach; 
return pdf;
        }

         }

Test Class written so far

@isTest 
public class TestMyQuoteListCntrlr{
    static testMethod void MyQuoteListCntrlrCons() 
    {
       Account  Acct = new Account();

       Acct.Name ='My Test Account';

       insert Acct;

       Opportunity Opp = new Opportunity ();

       // Do you recognize these fields?
       Opp.Name= 'Test';
       Opp.Account= Acct ;
       Opp.Amount= 0;
       Opp.CloseDate= System.today() ;
       Opp.StageName= 'Negotiation';

       insert Opp;




      Quote Qt= new Quote();


       // Do you recognize these fields?
       Qt.Name= 'Test';
       Qt.Opportunity= Opp ;
       Qt.Opportunityid= Opp.id ;
       Qt.Carrier__c= Acct.id;
       Qt.Account__c= Acct.id;
       Qt.Status= 'Carrier Approved' ;
       Qt.Billing_Type__c= 'Paid In Full';
       Qt.MonthlyPayments__c= 400;
       Qt.Premium_Amount__c= 300;
       Qt.DownPayment__c= 200 ;
       Qt.Down_Payment_Taken__c= 100;
       Qt.Of_Payments__c =10;
       Qt.Auto_Liability__c =True;
       Qt.Effective_Date_of_Policy__c =date.ValueOf('2016-02-02');
       Qt.DateQuoteReqSent__c =date.ValueOf('2016-02-02');
       Qt.DateQuoteReceived__c =date.ValueOf('2016-02-01');
     Qt.ExpirationDate =date.ValueOf('2016-02-01');
       insert Qt;


        Quote Qt1= new Quote();
        // Do you recognize these fields?
       Qt1.Name= 'Test1';
       Qt1.Opportunity= Opp  ;
       Qt1.Opportunityid= Opp.id ;
       Qt1.Carrier__c= Acct.id;
       Qt1.Account__c= Acct.id;
       Qt1.Status= 'Carrier Declined' ;
       Qt1.DeclinedReason__c      ='Chameleon Carrier';       

       insert Qt1;

        ApexPages.StandardController sc = new ApexPages.StandardController(opp);
        MyQuoteListCntrlr ctl = new MyQuoteListCntrlr ( sc); 


    }
    }
1
  • 1
    Add ctl.display() after you instantiated the controller in the test class
    – Prady
    Mar 4, 2016 at 8:50

1 Answer 1

1

Add ctl.display() after you instantiated the controller in the test class

ApexPages.StandardController sc = new ApexPages.StandardController(opp);
        MyQuoteListCntrlr ctl = new MyQuoteListCntrlr ( sc); 
ctl.display();

You must log in to answer this question.

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