I have been trying for about a week to figure out how to write a test method that will increase my code coverage and cannot figure it out. This is the code I have.
public class ConvertToVolunteerJob {
private Lead lead;
private Case newCase;
private Contact contact;
private Account account;
public ConvertToVolunteerJob(ApexPages.StandardController controller) {
lead = [SELECT
Id,
Status,
Existing_Contact_Record__c,
FirstName,
LastName,
Street,
City,
State,
PostalCode,
Country,
Email,
Phone,
Salutation,
Title,
Description,
Birthdate__c,
Emergency_Contact_Name__c,
Emergency_Contact_Phone_Number__c,
Gender__c,
Race__c,
Social_Security_Number__c,
LeadSource,
Barriers__c,
Referring_Agency_ID__c,
Disability__c,
Household_Veteran_Status__c,
Previous_Client__c,
Referral_Date__c,
Referring_Agency__c,
Referring_Counselor__c,
Service_Related_Disability__c,
Veteran_Status__c,
Program__c,
Intake_Type__c,
Enrolled_Programs__c,
Skills_Needed__c,
Campaign__c,
Location__c,
Total_Hours_Due__c,
Number_of_Hours_Due_Per_Week__c,
Name,
Primary_Point_of_Contact__c
FROM Lead
WHERE Id = :controller.getRecord().Id];
}
public PageReference cancel() {
PageReference pg = new PageReference('/' + lead.Id);
pg.setRedirect(true);
return pg;
}
public PageReference process() {
if (lead.Status == 'Closed - Converted to Case') {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'You cannot convert a Referral that has already been converted. Instead, clone this Referral, change the status to New, and then convert the new Referral to a Case.'));
return null;
}
if (lead.Existing_Contact_Record__c != null) {
contact = [SELECT Id, AccountId FROM Contact WHERE Id = :lead.Existing_Contact_Record__c];
account = new Account(Id = contact.AccountId);
} else {
causeview__BatchSettings__c settings = causeview__BatchSettings__c.getInstance('Default');
if (settings==null)
system.debug('batch setting was not found');
else {
account = new Account(Id=settings.causeview__BucketAccountId__c);
contact = new Contact(AccountId = settings.causeview__BucketAccountId__c);
}
}
contact.FirstName = lead.FirstName;
contact.LastName = lead.LastName;
contact.MailingStreet = lead.Street;
contact.MailingCity = lead.City;
contact.MailingState = lead.State;
contact.MailingPostalCode = lead.PostalCode;
contact.MailingCountry = lead.Country;
contact.Email = lead.Email;
contact.Phone = lead.Phone;
contact.Salutation = lead.Salutation;
contact.Title = lead.Title;
contact.Birthdate = lead.Birthdate__c;
contact.Emergency_Contact_Name__c = lead.Emergency_Contact_Name__c;
contact.Emergency_Contact_Phone_Number__c = lead.Emergency_Contact_Phone_Number__c;
contact.Gender__c = lead.Gender__c;
contact.Race__c = lead.Race__c;
contact.Social_Security_Number__c = lead.Social_Security_Number__c;
contact.LeadSource = lead.LeadSource;
upsert contact;
RecordType rc = [SELECT Id FROM RecordType WHERE Name = :lead.Program__c];
newCase = new Case(
RecordTypeId = rc.Id,
Related_Referral_Record__c = lead.Id,
Status = 'Open',
Opened_Date__c = Date.Today(),
Barriers__c = lead.Barriers__c,
Referring_Agency_ID__c = lead.Referring_Agency_ID__c,
Disability__c = lead.Disability__c,
Household_Veteran_Status__c = lead.Household_Veteran_Status__c,
Previous_Client__c = lead.Previous_Client__c,
Referral_Date__c = lead.Referral_Date__c,
Referring_Agency__c = lead.Referring_Agency__c,
Referring_Counselor__c = lead.Referring_Counselor__c,
Service_Related_Disability__c = lead.Service_Related_Disability__c,
Veteran_Status__c = lead.Veteran_Status__c,
Description = lead.Description,
Enrolled_Programs__c = lead.Enrolled_Programs__c,
ContactId = contact.Id,
AccountId = account.Id
);
lead.Existing_Contact_Record__c = contact.Id;
lead.Linked_Case__c = newCase.Id;
lead.Status = 'Closed - Converted to Case';
update lead;
RecordType intake = [SELECT Id FROM RecordType WHERE Name = 'Intake'];
Services_ASR__c service = new Services_ASR__c(
RecordTypeId = intake.Id,
Case__c = newCase.Id,
Date__c = Date.Today(),
Program_Type__c = lead.Program__c,
Type__c = lead.Intake_Type__c
);
GW_Volunteers__Volunteer_Job__c Volunteer_Job = new GW_Volunteers__Volunteer_Job__c(
GW_Volunteers__Campaign__c = lead.Campaign__c,
Location__c = lead.Location__c,
Total_Hours_Due__c = lead.Total_Hours_Due__c,
Number_of_Hours_Due_Per_Week__c = lead.Number_of_Hours_Due_Per_Week__c,
GW_Volunteers__Skills_Needed__c = lead.Skills_Needed__c,
Primary_Point_of_Contact__c = contact.id,
Name = contact.FirstName
);
insert Volunteer_Job;
PageReference pg = new PageReference('/a09/o');
pg.setRedirect(true);
return pg;
}
}
I honestly don't know if you are supposed to write test methods within the same class or create a whole new apex class for it. Any help would be much appreciated.