Thursday, October 16, 2014

Test Class for if and else conditions and fetching id from current VF page..

          =================CONTROLLER=======================
Public Class ifelsecondition {
// We can get the id from when click on Button
    public ID impId = ApexPages.currentPage().getParameters().get('id');
   
    public Id getImpId()
    {
        return impId;
    }
      
    public PageReference autoRun()
    {
   
       
        System.debug('    #### This is implementation ID' + impId );
        account tmpimp = [select name,phone from account where ID =: impId ];
              
   
               
        anotherclass  testimp = new WebServiceGCTImplementationclass();
        testimp.sfdcRequest();
        return postProcessing();
       
    }
   
    public PageReference postProcessing()
    {
        account imp = [select Name, phone,Rating from account where ID =:impId];
        if((imp.phone == null)|| (imp.phone != 'Success')){
            String errmsg = '';
            if (imp.phone == null)
            {
                errmsg = 'There was an error contacting GCT.  Please contact your administrator. (Implementation Name:'+imp.Name+')';
            }
            else
            {
                errmsg = 'There was an error: '+imp.phone+' : '+imp.GCT_Status__c ;
            }
            Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR,
                        errmsg ));
           
            return null;
        }       
        PageReference pageRef = new PageReference('/'+impId);
        pageRef.setRedirect(true);
        return pageRef;        
    }   
   
    
    public PageReference autoRun_EMEA()
    {
   
       
        System.debug('    #### This is implementation ID' + impId );
        opportunity tmpimp = [select Opportunity__c from opportunity where ID =: impId ];
              
   
               
        GCTWebserviceCallController populateService = new GCTWebserviceCallController();
        populateService.sendImpDetails();
        return postProcessing_EMEA();
       
    }
   
    public PageReference postProcessing_EMEA()
    {
        opportunity imp = [select Name, mobile,status from opportunity where ID =:impId];
        String errmsg = '';
        System.debug('***imp.phone '+imp.mobile);
        if((imp.mobile == null)
                || (imp.mobile != 'Success'))
        {
            //String errmsg = '';
            if (imp.mobile == null)
            {
                errmsg = 'There was an error contacting GCT.  Please contact your administrator. (Implementation Name:'+imp.Name+')';
            }
            else
            {
                errmsg = 'There was an error: '+imp.mobile+' : '+imp.status;
            }
            Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR,
                        errmsg ));
           
            return null;
        } 
           
        PageReference pageRef = new PageReference('/'+impId);
        pageRef.setRedirect(true);
        return pageRef;        
    }   
}
================Test Class===========================================
@isTest(SeeAllData=true)
Public Class TestclassForifelsecondition                   
{
     public static Id Imp1;
     public static  Id EMEA;
     public static string error='';
     public static testMethod void autorun(){
     // here we hard code one id from records and put in get('id') is same as in put('id')
   //                                                                                   
       ApexPages.currentPage().getParameters().put('id', 'a0NA0000001qKT9MAM');
       system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+Imp1);
       ifelsecondition cond = new ifelsecondition ();
       cond .getImpId();
       cond .autorun();
     }
     public static testMethod void autoRun_EMEA(){
        EMEA= ApexPages.currentPage().getParameters().put('id', 'a3uK00000000ZxXIAU');
        system.debug('######################################################'+EMEA);
        ifelsecondition cond = new ifelsecondition ();
        cond .autoRun_EMEA();
     }
     public static testMethod void postProcessing(){
       
        ApexPages.currentPage().getParameters().put('id', 'a0NA0000001qKT9MAM');
        ifelsecondition cond = new ifelsecondition ();
        cond .postProcessing();
       
     }
     public static testMethod void postProcessing_EMEA(){
        ApexPages.currentPage().getParameters().put('id', 'a3uK00000000aGAIAY');
        ApexPages.currentPage().getParameters().put('id', 'a3uA0000000EJMQIA4');
        ifelsecondition cond = new ifelsecondition ();
        cond .postProcessing_EMEA();
     }
  
}

No comments:

Post a Comment