+ Start a Discussion
BakTBakT 

Test Class for a custom convert class

I have a class that is used for converting a Lead. I need to write a test class for this but I can't seem to get it. Please help. Here's the class:

 

Public Class CustomLeadCont
{
string pr;
private final lead l;
public CustomLeadCont(ApexPages.StandardController stdController) {
this.l = (Lead)stdController.getRecord();
}
public PageReference autoRun() {

String theId = ApexPages.currentPage().getParameters().get('id');

if (theId == null) {
// Display the Visualforce page's content if no Id is passed over
return null;
}
for (Lead l:[select id, name, Checkbox_1__c, Checkbox_2__c from Lead where id =:theId])
{
If (l.Checkbox_1__c & l.Checkbox_2__c)
{
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(l.id);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
pr = lcr.getAccountId();
}
else{
pr = l.id;}
}
PageReference pageRef = new PageReference('/' + pr);
pageRef.setRedirect(true);
return pageRef;

}

}

----------------------------------------------------------------------------------------

Here's the Test Class:

 

@isTest
public class TestController{
static testMethod void TestControler()
{
Lead myLead = new Lead(LastName = 'Test', Company='Test Company', Checkbox_1__c = true, Checkbox_2__c=true);
insert myLead;
Apexpages.StandardController stdController = new Apexpages.StandardController(myLead);
CustomLeadCont cl= new CustomLeadCont(stdController);
cl.autoRun();
}
}

Best Answer chosen by Admin (Salesforce Developers) 
MattLacey.ax1065MattLacey.ax1065

I may be wrong, but the way you're initialising just using the standard controller doesn't mean the current page reference will have an id, therefore this code: 

if (theId == null) {
// Display the Visualforce page's content if no Id is passed over
return null;
}

 Is probably returning null and as such your coverage is low.

 

Modify your test method so that it reads like this:

 

Lead myLead = new Lead(LastName = 'Test', Company='Test Company', Checkbox_1__c = true, Checkbox_2__c=true);
insert myLead;

Pagereference sPage = new Pagereference('PageName');		
Test.setCurrentPageReference(sPage);

sPage.getParameters().put('id', myLead.Id);

Apexpages.StandardController stdController = new Apexpages.StandardController(myLead);

 

Let me know your mileage with that!

All Answers

MattLacey.ax1065MattLacey.ax1065

I may be wrong, but the way you're initialising just using the standard controller doesn't mean the current page reference will have an id, therefore this code: 

if (theId == null) {
// Display the Visualforce page's content if no Id is passed over
return null;
}

 Is probably returning null and as such your coverage is low.

 

Modify your test method so that it reads like this:

 

Lead myLead = new Lead(LastName = 'Test', Company='Test Company', Checkbox_1__c = true, Checkbox_2__c=true);
insert myLead;

Pagereference sPage = new Pagereference('PageName');		
Test.setCurrentPageReference(sPage);

sPage.getParameters().put('id', myLead.Id);

Apexpages.StandardController stdController = new Apexpages.StandardController(myLead);

 

Let me know your mileage with that!

This was selected as the best answer
BakTBakT

Thank you so much. So that's what I'm missing:

sPage.getParameters().put('id', myLead.Id);