0

I have the following Apex Class that I need to write a test class for. I've never done this before and rather new at this. Can someone offer pointers for this test class?

public class CurrentRecordIdController{

public String currentRecordId {get;set;}
public String parameterValue {get;set;}
public Lead lead{get;set;}

public String data {get;set;}
public Blob mac {get;set;}
public String sig {get;set;}
public String url {get;set;}
 
    public CurrentRecordIdController(ApexPages.StandardController controller) {
        currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        lead = [Select Id, Name, Email, Company from Lead where Id =: currentRecordId ];
        parameterValue = ApexPages.CurrentPage().getparameters().get('nameParam');

        data = currentRecordId;
        mac = Crypto.generateMac('HMacSHA256', Blob.valueOf(data), Blob.valueOf('key'));
        sig = EncodingUtil.urlEncode(EncodingUtil.base64Encode(mac), 'UTF-8');
        url = 'https://mysite.com/123456?lid=' + currentRecordId + '&signature=' + sig;

}

}

Thank you!

1 Answer 1

1

I will recommend you to start using trailhead to learn about test classes https://trailhead.salesforce.com/modules/apex_testing

Also please check below post

You write a test class for this the same way that you would any other:

  • Set up some data for the controller to access (in this case, Lead)
  • Execute a method/methods
  • Verify the behavior with asserts.
@IsTest
private class CurrentRecordIdControllerTest {

    //setup test data 
    @TestSetup
    static void setup() {
        Lead objLead = new Lead( FirstName = 'Test', LastName = 'Sample', Company = 'Testing Sample Co' );  
        insert objLead;  
    }


    @IsTest
    static void testController() {

        Lead l = [Select Id from Lead limit 1];
        Test.StartTest(); 
            ApexPages.StandardController sc = new ApexPages.StandardController(l);
            PageReference pageRef = Page.PageName; // Add your VF page Name here
            pageRef.getParameters().put('id', String.ValueOf(l.Id));
            pageRef.getParameters().put('nameParam', String.ValueOf('paramvalue'));
            Test.setCurrentPage(pageRef);
            CurrentRecordIdController leadController = new CurrentRecordIdController(sc);
            
        
        Test.StopTest();
    }

}
2
  • don't forget to mention using asserts - a good practice for the OP to get into from the get-go
    – cropredy
    Commented Nov 12, 2021 at 0:49
  • Thanks @GradientDecent for the thorough reply. I spent the weekend learning about test methods/classes and understand their purpose! Enjoy the week.
    – Jean-Se
    Commented Nov 15, 2021 at 13:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .