You need to sign in to do that
Don't have an account?
HELP -- Unit testing dilemma!
Hi,
I'm having a bit of trouble unit testing the following:
Class:
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.setLeadId(leadToConvert.Id);
lc.setAccountId(leadToConvert.account__c);
lc.setContactId(leadToConvert.contact__c);
lc.setDoNotCreateOpportunity(true); // There's no opportunity to create
lc.setOverwriteLeadSource(false);
lc.setSendNotificationEmail(false);
lc.setConvertedStatus(leadToConvert.status);
database.leadConvertResult lcr = Database.convertLead(lc);
system.assert(lcr.isSuccess());
PageReference retUrl = new PageReference ('/' + lcr.getContactId());
return (retUrl);
}
catch (exception e ) {
ApexPages.addMessages(e);
system.debug(e + ' got an error on the try-catch.');
Database.rollback(sp);
return null;
}
}
}
}
Unit Test so far:
@isTest
private class unitTest_leadConverter {
static testMethod void leadConverter_noOpp_test() {
Account testAccount = [select Id, Name from Account limit 1];
system.debug(testAccount);
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.assertNotEquals(insertLead.Account__c,null);
system.debug('insertLead.Id:' +insertLead.Id);
// call the page and add the lead Id
PageReference p2 = Page.leadConverterNoOpp;
system.debug('PageReference p2:' + p2);
Test.setCurrentPage(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);
Message:
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!!
Thanks!!
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());