1

I have an apex class.In the class i have method with url redirctions.Am unable to cover the method.Please guide me !!

Apex method

public PageReference urlRedirect() {

    system.debug('$<<Inside Urlredircet Method$>>' );
    String strServiceRecType = ApexPages.currentPage().getParameters().get('RecordType');

    if (strRecordId == null && strServiceRecType != null) {
        strDevName='';
        try {
            strDevName= [Select Id, Name, DeveloperName from Recordtype WHERE SObjectType = 'Service__c'  and Id = :strServiceRecType ].DeveloperName;      
        }catch (QueryException e){
            System.debug('Error' + e); 
        }
        if(strDevName!= 'RFLEET_Option_OPP') {

            String strReturn = '/a0G/e?nooverride=1&RecordType='+objService.RecordTypeId;
            if(objService.Opportunity_car_set__c != null) {
                system.debug('<<<<<objService.Opportunity_car_set__c '+objService.Opportunity_car_set__c);
                if(strCurrency!=null) { 

                strReturn += '&CF00N580000014eMw_lkid='+objService.Opportunity_car_set__c+'&CF00N580000014eMw='+[SELECT Name FROM Opportunity_car_set__c WHERE Id =: objService.Opportunity_car_set__c].Name+'&CurrencyIsoCode='+strCurrency+'&retURL=%2F'+objService.Opportunity_car_set__c; 
                } else { 
                strReturn += '&CF00N580000014eMw_lkid='+objService.Opportunity_car_set__c+'&CF00N580000014eMw='+[SELECT Name FROM Opportunity_car_set__c WHERE Id =: objService.Opportunity_car_set__c].Name+'&retURL=%2F'+objService.Opportunity_car_set__c; 
                }
                return new PageReference(strReturn);    
            } 
            if(objService.Car_Set__c!= null) {

            String strReturnCarSet = '/a0G/e?nooverride=1&RecordType='+objService.RecordTypeId;
            strReturnCarSet += '&CF00N580000014eMn_lkid='+objService.Car_Set__c+'&CF00N580000014eMn='+[SELECT Name FROM Rfleet_Car_Set__c WHERE Id =: objService.Car_Set__c].Name+'&retURL=%2F'+objService.Car_Set__c; 
            return new PageReference(strReturnCarSet );     
            }       
        } 
        if(strOppid==null && strCarsetid==null )     {  
            String strReturnService = '/a0G/e?nooverride=1&retURL=%2Fa0G%2Fo&RecordType='+objService.RecordTypeId;
            return new PageReference(strReturnService );     
        }
    } 
    return null;
}

test class

@isTest
public class Rfleet_ServiceCustomNew_VFCx_TEST {

    public static Account acc =new Account();
    public static Opportunity opp = new Opportunity(); 
    public static Product2 pModel = new Product2();  
    public static Product2 pVersion = new Product2();
    public static Opportunity_car_set__c oppCarSet = new Opportunity_car_set__c();
    public static Service__c  newServ= new Service__c();

 @testSetup static void testDataSetup(){

    Rfleet_allCountryInfo__c cs = new Rfleet_allCountryInfo__c(name= 'DZ' ,Rfleet_CountryName__c = 'Algeria' , Rfleet_C2GURL__c = 'http://dz.co.rplug.renault.com/range', Rfleet_Currency__c = 'DZD',Rfleet_priceType_Reference__c='PVCHT');
    insert cs;

     ID devAccRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('RFLEET-ACC-Algeria-RT').getRecordTypeId();
     acc.name='uma';
     acc.RecordTypeId =devAccRecordTypeId ;
     insert acc;    

     ID devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('RFLEET-OPP-Algeria-RT').getRecordTypeId();
      opp.name = 'uma';
     opp.stagename = 'Draft';
     opp.closedate = System.Today();
     opp.RecordTypeId = devRecordTypeId;
     opp.AccountId =acc.id;
     insert opp;
     System.assertEquals('uma',opp.name);

    pModel.Brand__c          = 'Renault';
    pModel.Name              = 'DZ-Nouvelle Latitude';
    pModel.Country_code__c   = 'DZ';
    pModel.Parent_Product__c = null;
    pModel.IsActive          = true;
    pModel.Market__c         = 'VP';
    pModel.ProductCode       = 'SFN';
    insert pModel;

    pVersion.Name                       = 'DZ-Nouvelle Latitude-Latitude Privilège Diesel';
    pVersion.Parent_Product__c          =  pModel.id;
    pVersion.Rfleet_C2G_version_code__c = 'VEC209_ALGE';
    pVersion.IsActive                   = true;
    insert pVersion;

    pModel = [Select id,Name,Brand__c,ProductCode,Parent_Product__c,isActive,Country_code__c,SCP_Label__c,Rfleet_C2GParentProduct__c  from Product2 where Id=:pModel.Id];

    oppCarSet.name                       = 'uma';
    oppCarSet.Brand__c                   = pModel.Brand__c;
    oppCarSet.Model__c                   = pModel.id;
    oppCarSet.Version__c                 = pVersion.id;
    oppCarSet.opportunity__c             = opp.id;
    oppCarSet.Quantity__c                = 2;
    oppCarSet.Price_HT__c                = 420.0;
    oppCarSet.Rfleet_TVN_Amount__c       = 89.7;
    insert oppCarSet;
    System.assertEquals(2,oppCarSet.Quantity__c);
    oppCarSet.Price_HT__c                = null;
    oppCarSet.Rfleet_TVN_Amount__c       = null;
    update oppCarSet; 

    ID devRecordTypeIdServ=  [Select id from RecordType where sObjectType = 'Service__c' and developerName ='RFLEET_Option_OPP' ].id;

     newServ.CodeC2G__c = 'QPA$MV-Peinture Métallisée ou nacrée';
     newServ.Code__c='QPA$MV';
     newServ.Label__c='Peinture Métallisée ou nacrée';
     newServ.Price_HT__c=5.0;
     newServ.RecordTypeId = devRecordTypeIdServ;
     newServ.Opportunity_car_set__c=oppCarSet.id;
     insert newServ;
 }


    static TestMethod void ServiceStandardTest () { 

        Test.Starttest(); 
        ApexPages.currentPage().getParameters().put('id',newServ.id);
        ApexPages.StandardController controller=new ApexPages.StandardController(newServ);
        Rfleet_ServiceCustomNew_VFCx servCls=new Rfleet_ServiceCustomNew_VFCx(controller);
        servCls.doSaveAndNew();
        servCls.urlRedirect();
        Test.StopTest(); 
    }

}
  • you just call those method which are returning pageReference and use assert to check the expected output. – Ratan Paul May 23 '16 at 6:20
  • am calling that method !!@Ratan – uma451 May 23 '16 at 6:23
  • If you are calling those methods then you need to create data based on your controller's if conditions so it should satisfy all the controller logic this way it will cover the lines. – Ratan Paul May 23 '16 at 6:25
  • for this line we have to add any data ? String strServiceRecType = ApexPages.currentPage().getParameters().get('RecordType'); – uma451 May 23 '16 at 6:27
  • 3
    So if this line is the problem here. then before calling your pagereference method you need to assign recordType in ccurrent page parameters. so you could do like this ApexPages.currentPage().getParameters().put('RecordType', objRecordType.Id) means in current page recordType is assigned. Now call your method then in method ApexPages.currentPage().getParameters().get('RecordType') this code will give you the recordType Id. – Ratan Paul May 23 '16 at 6:33
2

As @Ratan suggested ,by seeting the record type id as below it's covered,

static TestMethod void ServiceStandardTest () { 
        ID recTypeOptionOPP = [SELECT Id FROM RecordType 
                                WHERE SobjectType = 'Service__c'  
                                  AND DeveloperName = 'RFLEET_Option_ORD' ].ID;
        Test.Starttest(); 
        ApexPages.currentPage().getParameters().put('id',newServ.id);
        ApexPages.StandardController controller=new ApexPages.StandardController(newServ);
        Rfleet_ServiceCustomNew_VFCx servCls=new Rfleet_ServiceCustomNew_VFCx(controller);
        servCls.doSaveAndNew();
        PageReference pageRef = Page.Rfleet_ServiceCustomNew_VFP;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('RecordType', recTypeOptionOPP);
        servCls.strCurrency='DZD';
        servCls.urlRedirect();
        Test.StopTest(); 
    }

Thanks @Ratan

  • just for a side note don't forgot to add asserts to check if your expected result is correct or not. – Ratan Paul May 23 '16 at 10:46
0

Hey to cover that portion please try the below lines--->

Test.setCurrentPage(testPage);
Test.StartTest();
ControllerName controller = new ControllerName();
// call the urlRedirect and check the results of the pageReference.
pageReference p = controller.urlRedirect();
Test.StopTest();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.