0

I'm new to apex and stuck with below test class issue. Below is my apex method

VF code that calls method1 through actionFunction and apex:param

<apex:actionFunction name="deleteRec" action="{!method1}">
    <apex:param name="param" assignTo="{!recId}" value=""/>
</apex:actionFunction>

Method onLoad is referred in apex:repeat in VF page and displayed in the form of link. When the link is clicked, recordId__c(18 char) value is assigned to variable recId.

public class class1{
    public String recId{get;set;}
    public List<object1__c> recList = new List<object1__c>();
    public class1(ApexPages.StandardController controller) {
        recId = '';
        varId = controller.getId();
        onLoad();
    }
    public List<object1__c> onLoad() {

        recList = [SELECT field1__c, field2__c, recordId__c FROM object1__c WHERE Case__c =:varId];
        return recList;

    }
    public void method1(){
        List<object1__c> objList = new List<object1__c>();
        String recToDel = recId.substring(0, 15);
        objList = [SELECT Id, rec_ID__c FROM object1__c WHERE rec_ID__c =:recToDel];
        if(objList.size()>0){
            delete objList;
        }
    }
}

Note: value to recId is passed from VF page.

I'm getting System.StringException: Ending position out of bounds: 15 error while call method1 from my test class

@isTest
public class testclass{
    @isTest
    public static void method1Test(){
        //Inserting Sample case rec
        Case dummyCase = new Case();

        testCase.Subject = 'testCase Subject';
        testCase.Description = 'testCase Description';
        testCase.Status = 'Closed - Solved';
        testCase.Language__c ='English';
        insert testCase;

        object1__c objRec = new object1__c();
        objRec.field1__C__c = testCase.Id; 
        objRec.recordId__c = 'Ka1234567890123';
        objRec.field2__C= 'abc';

        insert objRec; 

        String recToDel = objRec.recordId__c.substring(0, 15);
        system.assertEquals('Ka1234567890123', recToDel);

        object1__c objRec = new object1__c();                
        objRec = [SELECT field1__c, recordId__c FROM object1__c WHERE recordId__c =:recToDel];
        delete objRec;
ApexPages.StandardController sc = new ApexPages.standardController(dummyCase);
        method1Class controller = new method1Class(sc);
        controller.method1(); // Getting error here
    }
}
  • Can you include the code where recId is set in your controller class please? I think that's the reason why you are getting this error. – Lance Shi Aug 8 at 5:02
  • is recId a URL param to the page? – cropredy Aug 8 at 5:05
  • I've updated both controller and test class code. – Ravi Teja Aug 8 at 5:33
0

You need to manually set your rec1 variable in your unit test method.

Something like:

method1Class controller = new method1Class(sc);
controller.recId = objRec.Id;
controller.method1(); 

Depending on what you want to test, you might want to set it to be a different sobject's id. But you got the idea here.

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.