+ 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');
    public PageReference onLoad() {
        List<Lead> Lead = [SELECT Id, Campaign_Name__c FROM Lead WHERE id=:ObjectId LIMIT 1];
        Lead[0].Campaign_Name__c = Code;
            UPDATE Lead;
           PageReference pr = new PageReference(PageR);
              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.

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

    public static testMethod void testSurveyRedirect()
        PageReference pageRef = Page.yourPage; //replace yourPage by your own vf page 
        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();
        //add assertion