-1

My Code coverage is 43%. I am not able to test the Method which returns PageReference method. In my case method is pass().

public static testmethod void test1()
    {    
      String strGUID1 = ApexPages.CurrentPage().getParameters().put('GUID','00000000');
      String strPage1 = ApexPages.CurrentPage().getParameters().put('page','abcd');
      CommonController objCreateServiceOrder = new CommonController();
      objCreateServiceOrder.strGUID = strGUID1;
      objCreateServiceOrder.strPage = strPage1;
      objCreateServiceOrder.pass();
      PageReference pg = Page.ViewQuote;
      pg.getParameters().put('val', 'A');
      pg.getParameters().put('id', 'sdsadsadas');
      pg.getParameters().put('GUID', '000000000');

      Test.setCurrentPage(pg);
      Test.stopTest();
    }

    -------------
 public PageReference pass()
{
    System.debug('inside pass');
    if(strPage != 'CreateCustomer'){
    for (integer i = 0 ; i <  GUID.size() ; i++   )
    {
        if (GUID[i].contains(option))
        {
            //system.debug('GUID[i]: ' + GUID[i] + ' OPTION : ' + option);
             if (GUID[i].contains('@'))
             {
                        String [] Prefixess =  GUID[i].split('@');
                        system.debug('Prefix[0]: ' + Prefixess[1]); 
                        if(Prefixess[0] == option){
                        preGUID = Prefixess[1];
                        break;
                        }
             }
             else
             {
                        preGUID = GUID[i];
                        //system.debug('GUID[i]: ' + GUID[i] + ' OPTION : ' + option);
                        break;
             }

        }
        }
    }

    PageReference pg;

    if (strPage.contains('ViewQuote'))
    {
         pg = new PageReference('/apex/ViewQuote?val=' + option+'&GUID='+preGUID+'&id='+ID);
    }
    else if (strPage.contains('InstantOrder'))
    {    
       pg = new PageReference('/apex/InstantOrder?val=' + option+'&GUID='+preGUID+'&id='+ID);
    }
    else if (strPage.contains('NewOrderNewQuote'))
    {
        pg = new PageReference('/apex/NewOrderNewQuote?val=' + option+'&GUID='+preGUID+'&id='+ID);
    }
    else if (strPage.contains('ViewOrderHistory'))
    {
        pg = new PageReference('/apex/ViewOrderHistory?val=' + option+'&GUID='+preGUID+'&id='+ID);
    }
    else if(strPage.contains('CreateCustomer')){
    System.debug('CreateCustomer : ' + option);
    if(option != null || option != ''){
    pg = new PageReference('/apex/SyncAccount?val=' + option + '&id=' + ID);
    System.debug('/apex/SyncAccount?val=' + option + '&id=' + ID);
    }
    }
    pg.setRedirect(true);
    return pg;
}

enter image description here

1

You need to set the value of "strPage" variable different type and the need to call pass() method every time. then that line will be covered.

public static testmethod void test1()
        {    
          String strGUID1 = ApexPages.CurrentPage().getParameters().put('GUID','00000000');
          String strPage1 = 



    ApexPages.CurrentPage().getParameters().put('page','abcd');
          CommonController objCreateServiceOrder = new CommonController();
          objCreateServiceOrder.strGUID = strGUID1;

          objCreateServiceOrder.strPage = 'ViewQuote';
          objCreateServiceOrder.pass();

          objCreateServiceOrder.strPage = 'InstantOrder';
          objCreateServiceOrder.pass();

          objCreateServiceOrder.strPage = 'NewOrderNewQuote';
          objCreateServiceOrder.pass();

          objCreateServiceOrder.strPage = 'ViewOrderHistory'";
          objCreateServiceOrder.pass();

          objCreateServiceOrder.strPage = strPage1;
          objCreateServiceOrder.pass();

          PageReference pg = Page.ViewQuote;
          pg.getParameters().put('val', 'A');
          pg.getParameters().put('id', 'sdsadsadas');
          pg.getParameters().put('GUID', '000000000');

          Test.setCurrentPage(pg);
          Test.stopTest();
        }
5
  • Thanks for the Response D-Horse, I copied the whole test method you provided but it is still on 43%. Still showing those red lines. – Mehul Aug 1 '14 at 12:30
  • I think now test class is covering your pagereferences and now you have to provide data to your class according to code for that you have write more test function. – D-Horse Aug 1 '14 at 13:05
  • I am passing all data from the test method and my all class properties are in blue, PageReference codes are in red. I also coverd all Methods. – Mehul Aug 1 '14 at 13:13
  • In your pass method there is variable strPage you need to provide the value like InstantOrder,strPage1 etc call pass method then again . – D-Horse Aug 1 '14 at 15:53
  • Can you please edit the answer , so I can get clear idea. – Mehul Aug 1 '14 at 16:26

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.