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
forcecloud20forcecloud20 

Need a test code coverage for redirect class...thank you in advance

public with sharing class AccRedirect {

private ApexPages.StandardController controller;
public String retURL {get; set;}
public String rType {get; set;}
public String rTypeName{get;set;}

public AccRedirect(ApexPages.StandardController controller) {

    this.controller = controller;

    retURL = ApexPages.currentPage().getParameters().get('retURL');
    rType = ApexPages.currentPage().getParameters().get('RecordType');
    rTypeName = [select named1 from recordtype where id=:rType].named1;

}

public PageReference redirect() {

    PageReference returnURL;

    IF(rTypeName == 'abc' || rTypeName == 'xyz') {

        returnURL = new PageReference('/apex/custPage');

    }

    ELSE {

        returnURL = new PageReference('/001/e');

    }

    returnURL.getParameters().put('retURL', retURL);
    returnURL.getParameters().put('RecordType', rType);
    returnURL.getParameters().put('rTypeName', rTypeName);
   
    returnURL.setRedirect(true);
    return returnURL;

}

}

Dhaval PanchalDhaval Panchal

This is a standard controller, so you need to provide which is the standard controller
I am giving you an example by considering standard controller is "Opportunity"

Below is the test class sample as per your class code, replace your object with "Opportunity".

 

@isTest
class TestSample
{
    static testMethod void myTest()
    {
        Opportunity opp = New Opportunity();
		opp.name = 'test';
		insert opp;
		
        ApexPages.StandardController controller = new ApexPages.StandardController(opp);
        ApexPages.CurrentPage().getParameters().put('retURL ','<put your any return url>'); //e.g. '%2F' + opp.id
        ApexPages.CurrentPage().getParameters().put('rType ','<put your value here>');
        AccRedirect clsTest = new AccRedirect(controller);
		
		clsTest.redirect();
    }
}

 Try and let me know it works for you or not.

forcecloud20forcecloud20

@isTest
Public class AccRedirectTest{
    static testmethod void testaccountredirect(){
    account acc = new account();
    acc.name = 'test';
    insert acc;

    ApexPages.StandardController sc= new ApexPages.standardController(acc);
   // ApexPages.CurrentPage().getParameters().put('retURL','/apex/testPage');

    ApexPages.CurrentPage().getParameters().put('retURL','%2F' +acc.id);
    //ApexPages.CurrentPage().getparameters().put('rType','abc');
   
    AccRedirect ar= new AccRedirect(sc);
   ar.redirect();
  
}

}

 

Updated test code accordingly, still it is giving error and test method is not passing. please correct me if iam wrong any where.

Dhaval PanchalDhaval Panchal
What error you are getting?
Dhaval PanchalDhaval Panchal
also let me know which lines are covered and which lines are not getting covered by test class.
forcecloud20forcecloud20

below is the error message

 

Error Message System.QueryException: List has no rows for assignment to SObject Stack Trace Class.AccountRedirect.<init>: line 22, column 1
Class.AccountRedirectTest.testaccountredirect: line 14, column 1

Dhaval PanchalDhaval Panchal

Use this code to cover all lines of your code.

 

@isTest
class TestSample
{
    static testMethod void myTest()
    {
		//scenario#1 for record type xyz and abc as per your code
		List<RecordType> lstRC = [Select id from name where name ='abc'];
        Account acc = New Account();
		acc.name = 'test';
		acc.recordTypeId = lstRc[0].Id;
		insert acc;
		
        ApexPages.StandardController controller = new ApexPages.StandardController(acc);
        ApexPages.CurrentPage().getParameters().put('retURL ','<put your any return url>'); //e.g. '%2F' + acc.id
        ApexPages.CurrentPage().getParameters().put('rType ',lstRc[0].Id);
        AccRedirect clsTest = new AccRedirect(controller);
		
		clsTest.redirect();
		
		//scenario#2 for record type other than xyz and abc as per your code
        Account acc1 = New Account();
		acc1.name = 'test';
		insert acc1;
		
        ApexPages.StandardController controller1 = new ApexPages.StandardController(acc1);
        ApexPages.CurrentPage().getParameters().put('retURL ','<put your any return url>'); //e.g. '%2F' + acc.id
        //ApexPages.CurrentPage().getParameters().put('rType ',lstRc[0].Id); // don't pass this value to cover your elase part of code
        AccRedirect clsTest1 = new AccRedirect(controller1);
		
		clsTest1.redirect();
    }
}

 

 

Let me know if you face any issue.

Dhaval PanchalDhaval Panchal
can you please show which line is at line 22 and 14?
I think error is coming from class. please show entire code of your class and test class with this line no.
forcecloud20forcecloud20

Hi Dhaval,

 

Thanks for your help, i think i am almost near to completion. Still facing below issue while executing.

---------------------------------------------------------------------------

System.QueryException: List has no rows for assignment to SObject
Stack Trace Class.AccountRedirect.<init>: line 22, column 1
Class.AccountRedirectTest.testAccountred: line 26, column 1

-------------------------------------------------------------------------

 

 

@isTest
public class AccredirTest{

    public static testmethod void testAccountred()
    {
        
       List<RecordType> lstRC = [Select id from recordtype where name ='Customer'];
      
       Account acc = new Account();
       acc.name = 'Test';
       acc.recordtypeid = lstRc[0].Id;
       insert acc;
             
       
        ApexPages.StandardController controller = new ApexPages.StandardController(acc);
       
        ApexPages.CurrentPage().getParameters().put('retURL','/apex/AcctCustPage?id='+acc.id);
        ApexPages.CurrentPage().getParameters().put('rType ',lstRc[0].Id);
        ApexPages.CurrentPage().getParameters().put('cancelURL','https://cs7.salesforce.com/001/o');
        ApexPages.CurrentPage().getParameters().put('ent','Account');
        ApexPages.CurrentPage().getParameters().put('_CONFIRMATIONTOKEN','true');
        ApexPages.CurrentPage().getParameters().put('saveNewURL','https://cs7.salesforce.com/001/o');
        ApexPages.CurrentPage().getParameters().put('nooverride','1');
        ApexPages.CurrentPage().getParameters().put('rTypeName ','Customer');
       
        Accredir clsTest = new Accredir(controller);
        clsTest.redirect();

    }

}