Cover the constructor in the test class of the salesforce

apexunit-test

I have completed almost around 71 percent of my test coverage ,,but not sure how to cover the constructor in my test class can anyone help me out.

Below is my constructor.

public String oppId {get;set;}
    public testController() {
        oppId = ApexPages.currentPage().getParameters().get('id');
    }

Best Answer

It's the same as you'd do for any other method:

@isTest static void myTest() {
  // Set up test data
  Opportunity record = TestUtils.createTestOpportunity();
  // Set any parameters you need
  ApexPages.currentPage().getParameters().put('id', record.Id);
  Test.startTest();
  // new X() calls the relevant constructor
  testController controller = new testController();
Related Topic