+ Start a Discussion

HELP -- Unit testing dilemma!



I'm having a bit of trouble unit testing the following:




public class leadConverternoOpp {

public class applicationException extends Exception {}

public Lead leadToConvert {get; private set;}

public Lead getLead() {

Id leadId = ApexPages.currentPage().getParameters().get('leadId');
leadToConvert = (leadId == null) ? new Lead() : [select id, Contact__c, Status, Account__r.Name ,Account__c,Opportunity__c, Name, LeadSource
from Lead where id = :leadId];

return leadToConvert;


* Constructor fetches the lead

public leadConverternoOpp(ApexPages.StandardController controller) {

this.leadToConvert = (Lead)controller.getRecord();


public PageReference cancel() {

PageReference retUrl = new PageReference ('/' + leadToConvert.Id);
return (retUrl);

public PageReference convertLeadNoOpp_1() {

return Page.leadConverterNoOpp;

public PageReference convertLeadNoOpp_2() {

Savepoint sp = Database.setSavepoint();

if (leadToConvert.Opportunity__c == null
&& leadToConvert.Status == 'Part of an Existing Opportunity') {

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,
'Please include the Opportunity below'));
return Apexpages.currentPage();

else if (leadToConvert.Opportunity__c != null
&& leadToConvert.Status != 'Part of an Existing Opportunity'){

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,
'"Part of an Existing Opportunity" was not chosen but an Opportunity was supplied'));
return Apexpages.currentPage();

} else {

try {

database.Leadconvert lc = new database.Leadconvert();
lc.setDoNotCreateOpportunity(true); // There's no opportunity to create

database.leadConvertResult lcr = Database.convertLead(lc);
PageReference retUrl = new PageReference ('/' + lcr.getContactId());
return (retUrl);
catch (exception e ) {
system.debug(e + ' got an error on the try-catch.');
return null;







Unit Test so far:



private class unitTest_leadConverter {

static testMethod void leadConverter_noOpp_test() {

Account testAccount = [select Id, Name from Account limit 1];

Contact testContact = new Contact(
Firstname = 'Test',
Lastname = 'Test',
Email = 'Test1@xyz.com',
AccountId = testAccount.Id);

insert testContact;

Lead insertLead = new Lead(
Firstname = 'Test',
Lastname = 'Test',
Company = 'TestCo',
Email = 'Test1@xyz.com',
Account__c = testAccount.Id,
Contact__c = testContact.Id);

insert insertLead;
system.debug('insertLead.Id:' +insertLead.Id);

// call the page and add the lead Id
PageReference p2 = Page.leadConverterNoOpp;
system.debug('PageReference p2:' + p2);
ApexPages.currentPage().getParameters().put('leadId', insertLead.Id);

ApexPages.Standardcontroller sc;
leadConverternoOpp controller = new leadConverternoOpp(sc);

PageReference nextPage = controller.convertLeadNoOpp_2();

system.debug('nextPage:' + nextPage);




The unit test creates a lead, then adds that lead to the URL so that the class can pull up the lead and then convert it.  The problem is that this line is returning a null pointer exception.

leadConverternoOpp controller = new leadConverternoOpp(sc);



System.NullPointerException: Attempt to de-reference a null object


Stack Trace:

Class.leadConverternoOpp.<init>: line 25, column 42 Class.unitTest_leadConverter.leadConverter_noOpp_test: line 93, column 37



Is this code majorly ill or just slightly ill?


Any kind of help is appreciated as this is giving me more gray hair!!




Message Edited by Ron-LON on 09-18-2009 06:03 PM
Message Edited by Ron-LON on 09-18-2009 06:04 PM
Best Answer chosen by Admin (Salesforce Developers) 

I believe you need to instantiate the standard controller. The line above the line in question should be changed:



ApexPages.standardController sc = new ApexPages.standardController(new Lead());