+ Start a Discussion
Sam WardSam Ward 

Can anyone help me write a test class?

public class SurveyRedirect {

    public String ObjectId {get;set;}
    public String Code {get;set;}
    public String PageR {get;set;}
    
    public SurveyRedirect () {
        ObjectId = ApexPages.currentPage().getParameters().get('id');
        Code = ApexPages.currentPage().getParameters().get('Code');
       PageR = ApexPages.currentPage().getParameters().get('PageR');
        system.debug(ObjectId);
    }
    public PageReference onLoad() {
        List<Lead> Lead = [SELECT Id, Campaign_Name__c FROM Lead WHERE id=:ObjectId LIMIT 1];
        if(!Lead.IsEmpty()){
        Lead[0].Campaign_Name__c = Code;
            UPDATE Lead;
            }
           PageReference pr = new PageReference(PageR);
              pr.setRedirect(true);
              return pr;
        }
    }
I'm new to Apex so I dont know how to write a test class any help on how to write them would be much appericated.

Thanks
Best Answer chosen by Sam Ward
David Zhu 🔥David Zhu 🔥
You may use the following code snipet for your reference.
@isTest
public class SurveyRedirectTest {

    public static testMethod void testSurveyRedirect()
    {
        PageReference pageRef = Page.yourPage; //replace yourPage by your own vf page 
		Test.setCurrentPage(pageRef);
        ApexPages.currentPage().setParameters().put('id', 'xxxxxx');  //replace xxxx by your own value
        ApexPages.currentPage().setParameters().put('Code', 'xxxxxx'); //replace xxxx by your own value
        ApexPages.currentPage().setParameters().put('PageR', 'xxxxxx'); //replace xxxx by your own value
        Lead l = new Lead();
        l.firstname ='test';
        l.lastName = 'test';
		insert l;    
        SurveyRedirect sr = new SurveyRedirect();
        sr.onLoad();
        
        //add assertion
    }
    

}