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
Sachin Sharma 3886Sachin Sharma 3886 

Test class. urgent help required

He Champs,

I am working on some test class and help required. Below is my Apex class and Test class code.
__________________________
 public with sharing class NeMO_InvoiceOverviewcontroller {
 
    public invoice__c objInvoice{get; set;}
    public Asset objAsset{get; set;}
    public string vehicleId{get;set;}
    public list<invoice__c> lstWrapperCommission{get; set;}
    public list<invoice__c> lstWrapperSuplimentry{get; set;}
    public list<invoice__c> lstWrapperVehicleInvoice {get; set;}
    public general_parameters__c generalParameter{get; set;}
    public id recordtypeIdsupli; 
    public id recordtypeIdvehicle;
    public id recordtypeIdcommission; 
    public decimal TotalNet {get; set;}
    public decimal TotalVatFFO {get; set;}
    public decimal SubToatl {get; set;}
    public decimal TotalNetDFO {get; set;}
    public decimal TotalVatDFO {get; set;}
    public decimal SubToatlDFO {get; set;}
    public list<wrapperStatus> lstWrapStatus{get; set;}
    public boolean vehicleStatus{get;set;}  // this  value is used for check the document storage present in vehicle invoice header or not
    public boolean commisionStatus{get;set;}  // this  value is used for check the document storage present in  Commission or not
    public string isexist {get; set;}
    public boolean isSuppexist {get ;set;}
    public string strSelectedSuppInvoice {get; set;}
    private static final string URLOVERVIEW = '/apex/NeMo_invoiceOverview?VehicleId=';
    private static final string URLSUPPLEMENTRY = '/apex/NeMO_SupplementaryInvoice?VehicleId=';
    
    //To Mantain Stage
    private static final String STRSTAGEINV ='Invoice Breakdown';
    private Set<String> setStage;
    NEMO_Invoice_Util utility=new NEMO_Invoice_Util();
     
    /**************************************************************************
    *   @Name        :  NeMO_InvoiceOverviewcontroller                        *
    *   @Params      :  void                                                  *                       
    *   @Return      :  void                                                  *                                  
    *   @Description :  Constructor                                           *
    **************************************************************************/
   
   
        public NeMo_InvoiceOverviewcontroller(){
         
            vehicleId = ApexPages.currentPage().getParameters().get('VehicleId');
            isexist = ApexPages.currentPage().getParameters().get(label.NEMO_ISEXISTPARAM);
             generalParameter = general_parameters__c.getInstance(); 
             vehicleStatus=false;
            recordtypeIdsupli = NeMo_GlobalUtill.GetRecordTypeId('invoice__c','Supplementar Invoice Header');    
            recordtypeIdvehicle = NeMo_GlobalUtill.GetRecordTypeId('invoice__c','Vehicle Invoice Header');
             recordtypeIdcommission = NeMo_GlobalUtill.GetRecordTypeId('invoice__c','Commission Invoice Header');
             onLoadData();
             StatusDocument();
             StatusDocumentForVehComm();
             
            
        }
     
    /**************************************************************************
    *   @Name        :  NeMO_InvoiceOverviewcontroller                        *
    *   @Params      :  void                                                  *                       
    *   @Return      :  void                                                  *                                  
    *   @Description :  This Method is used to load the data on page          *
    **************************************************************************/
    
        private void onLoadData(){  
            lstWrapperCommission = new list<invoice__c>();
            lstWrapperSuplimentry = new list<invoice__c>();
            lstWrapperVehicleInvoice = new list<invoice__c>();
            isSuppexist  = false;
            TotalNet = 0;
            TotalVatFFO = 0;
            SubToatl = 0;
            TotalNetDFO = 0;
            TotalVatDFO = 0;
            SubToatlDFO = 0;
            
            if(vehicleId != null && vehicleId != '')
            {
                 for(invoice__c InvFor:[select id, name,status__c ,line_item_type__c,supplier__r.Name,Subtotal__c,total_net__c,
                 Recordtypeid,recordtype.name,total_vat__c,total_vat_amount__c,total_net_amount__c,total_gross_amount__c,
                 (select id,Name,total_net__c,subtotal__c,total_vat__c,line_item_type__c  from Invoice_Line_Items__r) 
                from invoice__c where asset__c=:vehicleId])
                 
                {
                     if(InvFor.recordtype.name == 'Vehicle Invoice Header')
                     {
                         lstWrapperVehicleInvoice.add(InvFor);
                         if(InvFor.Invoice_Line_Items__r.size() > 0)
                         {
                             for(invoice_line_item__c objInvoiceLine : InvFor.Invoice_Line_Items__r)
                             {
                                 if(objInvoiceLine.line_item_type__c == 'Factory Fitted Option (FFO)'){
                                     if(objInvoiceLine.total_net__c != null){
                                         TotalNet += objInvoiceLine.total_net__c;
                                     }
                                     if(objInvoiceLine.total_vat__c != null){
                                         TotalVatFFO += objInvoiceLine.total_vat__c ;
                                     }
                                     if(objInvoiceLine.subtotal__c != null){
                                         SubToatl += objInvoiceLine.subtotal__c;
                                     }
                                 }
                                 if(objInvoiceLine.line_item_type__c == 'Dealer Fitted Option (DFO)')
                                 {
                                     if( objInvoiceLine.total_net__c != null){
                                        TotalNetDFO += objInvoiceLine.total_net__c;
                                    }
                                    if(objInvoiceLine.total_vat__c != null){
                                        TotalVatDFO += objInvoiceLine.total_vat__c ;
                                    }
                                    if(objInvoiceLine.subtotal__c != null){
                                        SubToatlDFO += objInvoiceLine.subtotal__c;
                                    }
                                 }
                             }
                         }
                     }
                     if(InvFor.recordtype.name == 'Supplementar Invoice Header')
                     {
                         lstWrapperSuplimentry.add(InvFor);
                     }
                     if(InvFor.recordtype.name == 'Commission Invoice Header')
                     {
                         lstWrapperCommission.add(InvFor);
                     }
                 } 
                 if(isexist == NeMO_GlobalConstant.PARAMONE || isexist == NeMO_GlobalConstant.PARAMZERO){
                     isSuppexist = true;
                 }  
            }   
        }
         /**
        * This Method is used to ........... .
        * @name : 
       */ 
        public pagereference InvoicingOverview(){
            PageReference pg = new PageReference(URLOVERVIEW +vehicleId+'&ovId=0');
            pg.setRedirect(true);
            return pg; 
        }
        
        public pagereference redirectToSupplier(){
            system.debug('Ankita===='+strSelectedSuppInvoice);
            PageReference pg = new PageReference(URLSUPPLEMENTRY+vehicleId+'&SuppId='+strSelectedSuppInvoice+'&isexist='+isexist);
            pg.setRedirect(true);
            return pg;
           
        }
        
        public void Close(){
            // Call this method to update stage in Invoice to identify new or existing credit proposal 
            if(!String.isBLANK(vehicleId) && vehicleId !=NeMO_GlobalConstant.PARAMNULL){
                utility.UpdateInvoiceStage(vehicleId ,STRSTAGEINV);
            } 
        }
        
        /**
            * This Method is used to load the data on page .
            * @name : document 
        */
    /************************************************************************************
    *   @Name        :  NeMO_InvoiceOverviewcontroller                                  *
    *   @Params      :  void                                                            *                       
    *   @Return      :  void                                                            *                                  
    *   @Description :  This Method is used to change the status the sent os not sent   *
    *************************************************************************************/
    
        private void StatusDocumentForVehComm(){
            list<invoice__c> lstfor_veh_comm =new list<invoice__c>();
            if(vehicleId != null && vehicleId != ''){
                lstfor_veh_comm =[SELECT id,name,Recordtypeid,Recordtype.name,(SELECT id,invoice__c,GDMS_Document_Id__c FROM Document_Storage__r)
                             FROM invoice__c WHERE asset__c=:vehicleId AND Recordtype.name IN('Vehicle Invoice Header','Commission Invoice Header')];
                IF(lstfor_veh_comm.size()>0){
                    For(invoice__c invo:lstfor_veh_comm){
                        For(GDMS_Document__c docu: invo.Document_Storage__r){
                            if(invo.Recordtype.name == 'Vehicle Invoice Header' && docu.invoice__c == invo.id){
                                if(docu.GDMS_Document_Id__c != null && docu.GDMS_Document_Id__c != ''){
                                    vehicleStatus=true;
                                }
                            }
                            if(invo.Recordtype.name == 'Commission Invoice Header' && docu.invoice__c == invo.id){
                                if(docu.GDMS_Document_Id__c !=null && docu.GDMS_Document_Id__c != ''){
                                    commisionStatus=true;
                                 }
                            }
                        }
                    }
                }  
            }
        }
        
        /************************************************************************************
        *   @Name        :  NeMO_InvoiceOverviewcontroller                                  *
        *   @Params      :  void                                                            *                       
        *   @Return      :  void                                                            *                                  
        *   @Description :  This Method is used to change the status the sent os not sent   *
        *************************************************************************************/
        
        private void StatusDocument(){
            list<invoice__c> lstofInvoice  = new list<invoice__c>();
            lstWrapStatus = new  list<wrapperStatus>();
            if(vehicleId != null && vehicleId != ''){
                lstofInvoice =[SELECT id,name,total_net_amount__c,supplier__r.Name,Recordtypeid,Recordtype.name,(SELECT id,invoice__c,GDMS_Document_Id__c FROM Document_Storage__r)
                               FROM invoice__c WHERE asset__c=:vehicleId AND Recordtype.name = 'Supplementar Invoice Header'];
                IF(lstofInvoice.size()>0){
                    for(invoice__c invoie : lstofInvoice ){
                        if(!invoie.Document_Storage__r.isEmpty()){
                            for(GDMS_Document__c docu: invoie.Document_Storage__r){
                                wrapperStatus  objWrapStatus = new wrapperStatus();
                                if(docu.invoice__c == invoie.id){
                                    if(docu.GDMS_Document_Id__c !=null && docu.GDMS_Document_Id__c != '')
                                             objWrapStatus.supplementarStatus=true;
                                             objWrapStatus.objInvoice = invoie;
                                }
                                lstWrapStatus.add(objWrapStatus);
                            }
                        }
                    }
                }       
            }
        }
       
        
        /****************************************************************************************
        *   @Name        :  NeMO_InvoiceOverviewcontroller                                      *
        *   @Params      :  void                                                                *                       
        *   @Return      :  void                                                                *                                  
        *   @Description :  This wrapper class is used to display Status of Invoice Line Item   *
        *****************************************************************************************/
        
        public class wrapperStatus{
            public invoice__c objInvoice {get; set;}
            public boolean supplementarStatus{get;set;}
            public wrapperStatus(){
            this.supplementarStatus=false;
            }
        }       
}
__________________

Test class code
___________

@istest
private class NeMO_InvoiceOverviewcontroller_Test{

static testMethod void MyTest(){


Account acc= NeMO_TestUtil.createTestAccountsSupplier(true);
insert acc;
Contact c = NeMO_TestUtil.createTestContactswithAcc( true,acc.id);
insert c;
Asset ast= NeMO_TestUtil.createTestAsset_ConAccFirst(true,c.id,acc.id );
insert ast;
system.debug('***' + ast);
Invoice__c Inv = NeMO_TestUtil.createTestInvoiceAsset(true,ast.id,'Supplementar Invoice Header');
insert inv;

Test.startTest(); 
NeMO_InvoiceOverviewcontroller obj = new NeMO_InvoiceOverviewcontroller();
//system.debug('*Sachin*'+ obj.vehicleId);
obj.vehicleId=ast.id;

obj.InvoicingOverview();
obj.redirectToSupplier();
test.stoptest();

}
}

Test class is not mathcing the vehicleId  condition in onLoadData(), its showing as null, however i am passing the same from the test class.

Please help.
____________
James LoghryJames Loghry
Before you call your controller, you'll need to set the page and parameter for the VehicleId, since your constructor reads that value and passes it through to the onLoadData method.  You can do this using the following snippet:
 
static testMethod void MyTest(){


    Account acc= NeMO_TestUtil.createTestAccountsSupplier(true);
    insert acc;

    Contact c = NeMO_TestUtil.createTestContactswithAcc( true,acc.id);
    insert c;

    Asset ast= NeMO_TestUtil.createTestAsset_ConAccFirst(true,c.id,acc.id );
    insert ast;

    system.debug('***' + ast);
    Invoice__c Inv = NeMO_TestUtil.createTestInvoiceAsset(true,ast.id,'Supplementar Invoice Header');
    insert inv;

    PageReference pageRef = new PageReference('someUrl');
    pageRef.getParameters().put('VehicleId',ast.Id);
    Test.setCurrentPage(pageRef);

    Test.startTest(); 
    NeMO_InvoiceOverviewcontroller obj = new NeMO_InvoiceOverviewcontroller();
    obj.vehicleId=ast.id;

    obj.InvoicingOverview();
    obj.redirectToSupplier();
    test.stoptest();
}

Make sure you also add assertions afterward.