[SalesForce] Non static method invocation in static test method

I have a controller class where I have a non static field leadObj which is initialized in constructor with parameter:

// Constructor
public LeadToMerchantController(ApexPages.StandardController controller) {    
    objLead = (Lead)controller.getRecord();
    System.debug('objLead ' + objLead.Id);   
}

I have two non static methods where the leadObj is used and from two testMethods in a TestClass I want to start and test the two methods. Here is how the testMetdhods are implemented:

static testMethod void testLeadStatusExistingDeal() {
    Test.startTest();
    Lead leadObj = new Lead(LastName = 'test', Currency__c = 'USD', Company ='test', Monthly_Volume__c= '1234', Phone='1423542452', Website='www.google.com', Status = '0% Dead Lead');
    System.debug('leadObj ' + leadObj);
    String leadStatusExistingDeal = LeadToMerchantController.leadStatusExistingDeal(leadObj);
    System.debug('leadStatusExistingDeal ' + leadStatusExistingDeal);
    Test.stopTest();
}

static testMethod void testConvertOrRedirect(){
    Test.startTest();
    LeadToMerchantController.convertOrRedirect();
    Test.stopTest();
}

the two methods in the LeadToMerchantController class can not be invoked directly(calssName.methodName) because they are not static.

It is not possible to make an object from this LeadToMerchantController class because the constructor has a specific parameter public LeadToMerchantController(ApexPages.StandardController controller)and it is not clear what to pass for parameter.

How can I invoke from the test method the other method LeadToMerchantController.convertOrRedirect(); which has to be tested?

Best Answer

You simply use an ApexPages.StandardController:

Lead record = new Lead(...);
insert record;
ApexPages.StandardController c = new ApexPages.StandardController(record);
LeadToMerchantController ltmc = new LeadToMerchantController(c);

You can then proceed testing your other methods from here.

Related Topic