+ Start a Discussion
abinayaabinaya 

need help to increase the code coverage

 Hi,

 

Please find the below trigger and test class. As of now the code coverage is only 46% and i have passed all the parameters(mandatory) field values. Plz advice.

 

trigger statusupdate on Candidate_Mapping__c (before insert, before update)
{

public string strConvertedDate {get; set;}

if(Trigger.isUpdate)
{
// Candidate_Mapping__c ac = Trigger.new[0];
for(Candidate_Mapping__c ac : Trigger.new)
{
system.debug('ac:'+ ac);

candidate_mapping__c ac1 = Trigger.oldMap.get(ac.ID);

Map<Id,Candidate_Mapping__c> newContactMap = Trigger.newMap;
Map<Id,Candidate_Mapping__c> oldContactMap = Trigger.oldMap;

system.debug('newContactMap:' + newContactMap );
system.debug('oldContactMap:' + newContactMap );

if (ac.LR_Status__c == 'Approved' && ac.LR_Status__c != ac1.LR_Status__c )
{
// ac.LR_Email__c = ac.LR_Email_ID__c;

ac.status1__c = 'LR Approved';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
ac.LR_Status_Date__c = DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.LR_Status__c == 'Rejected'&& ac.LR_Status__c != ac1.LR_Status__c)
{
// ac.LR_Email__c = ac.LR_Email_ID__c;
ac.status1__c = 'LR Rejected';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
ac.LR_Status_Date__c = DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}



if (ac.MR_Status__c == 'Approved' && ac.MR_Status__c != ac1.MR_Status__c)
{
//
for(Id contactId:newContactMap.keySet())
{
//Loop through the map
Candidate_Mapping__c myNewContact = newContactMap.get(contactId);
Candidate_Mapping__c myOldContact = oldContactMap.get(contactId);
if (myNewContact.MR_Status__c <> myOldContact.MR_Status__c)
{
ac.MR_Status1__c = 'Approved';
ac.MR_Status_Date__c = DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');

}
else
{
ac.MR_Status1__c = 'None';
}
}
//
//ac.MR_Status1__c = 'Approved';
ac.status1__c = 'Approved';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
ac.MR_Status_Date__c = DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.MR_Status__c == 'Rejected' && ac.MR_Status__c != ac1.MR_Status__c)
{
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
ac.status1__c = 'Rejected';
ac.MR_Status_Date__c = DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.Submitted_to_Client__c == 'Yes' && ac.Submitted_to_Client__c != ac1.Submitted_to_Client__c)
{
ac.status1__c = 'Submitted to Client';
ac.Priority__c = 'a';
ac.Submitted_to_Client_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.Submitted_to_Client__c == 'No'&& ac.Submitted_to_Client__c != ac1.Submitted_to_Client__c)
{
ac.status1__c = 'Not Submitted to Client';
ac.Priority__c = 'a';
ac.Submitted_to_Client_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.Interview_Scheduled__c == 'Yes'&& ac.Interview_Scheduled__c != ac1.Interview_Scheduled__c )
{
ac.status1__c = 'Interview Requested';
ac.Priority__c = 'b';
ac.Interview_Scheduled_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.Interview_Scheduled__c == 'No' && ac.Interview_Scheduled__c != ac1.Interview_Scheduled__c )
{
ac.status1__c = 'Not Shortlisted For Interview';
ac.Priority__c = 'b';
ac.Interview_Scheduled_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.Interview_Accepted__c == 'Yes' && ac.Interview_Accepted__c != ac1.Interview_Accepted__c)
{
ac.status1__c = 'Interview Accepted';
ac.Priority__c = 'c';
ac.Interview_Accepted_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.Interview_Accepted__c == 'No' && ac.Interview_Accepted__c != ac1.Interview_Accepted__c)
{
ac.status1__c = 'Candidate Not Available';
ac.Priority__c = 'c';
ac.Interview_Accepted_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.Client_Interviewed__c == 'Yes'&& ac.Client_Interviewed__c != ac1.Client_Interviewed__c)
{
ac.status1__c = 'Client Interviewed';
ac.Priority__c = 'd';
ac.Client_Interview_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.Client_Interviewed__c == 'No' && ac.Client_Interviewed__c != ac1.Client_Interviewed__c)
{
ac.status1__c = 'Client Did Not Interview';
ac.Priority__c = 'd';
ac.Client_Interview_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.Client_Offered__c == 'Yes' && ac.Client_Offered__c != ac1.Client_Offered__c)
{
ac.status1__c = 'Client Offered';
ac.Priority__c = 'e';
ac.Client_Offer_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.Client_Offered__c == 'No' && ac.Client_Offered__c != ac1.Client_Offered__c)
{
ac.status1__c = 'Interviewed and rejected';
ac.Priority__c = 'e';
ac.Client_Offer_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.Candidate_started__c == 'Yes' && ac.Candidate_started__c != ac1.Candidate_started__c)
{
ac.status1__c = 'Candidate started';
ac.Priority__c = 'f';
ac.started_date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.Candidate_started__c == 'No' && ac.Candidate_started__c != ac1.Candidate_started__c )
{
ac.status1__c = 'Candidate did not start';
ac.Priority__c = 'f';
ac.started_date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

system.debug('ac:'+ ac);
system.debug('Status:' + ac.status1__c);
system.debug('Priority:' + ac.Priority__c);
system.debug('Manager Email:' + ac.Manager_Email_ID__c);
system.debug('LR_Email__c :' + ac.LR_Email__c);
system.debug('R_Email__c :' + ac.R_Email__c );
system.debug('MR_Status1__c:' + ac.MR_Status1__c );

}
}

if(Trigger.isInsert)
{
//Candidate_Mapping__c ac = Trigger.new[0];
for(Candidate_Mapping__c ac : Trigger.new)
{
system.debug('ac:'+ ac);

//Map<Id,Candidate_Mapping__c> newContactMap = Trigger.newMap;
//Map<Id,Candidate_Mapping__c> oldContactMap = Trigger.oldMap;


system.debug('LRStatus:' + ac.LR_Status__c);
system.debug('MRStatus:' + ac.MR_Status__c);

if(ac.Status1__c == 'Applied')
{
// ac.R_Email__c = ac.R_email_id__c;
ac.Requirement_Owner_Email__c = ac.Req_Owner_Email__c;
// ac.Manager_Email_ID__c = ac.LR_Email_ID__c;
}

if (ac.LR_Status__c == 'Approved')
{
// ac.LR_Email__c = ac.LR_Email_ID__c;
ac.status1__c = 'LR Approved';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
ac.LR_Status_Date__c = DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.LR_Status__c == 'Rejected')
{

// ac.LR_Email__c = ac.LR_Email_ID__c;
ac.status1__c = 'LR Rejected';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
ac.LR_Status_Date__c = DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}


if (ac.MR_Status__c == 'Approved')
{
ac.MR_Status1__c = 'Approved';
ac.status1__c = 'Approved';
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
ac.MR_Status_Date__c = DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

if (ac.MR_Status__c == 'Rejected')
{
ac.Manager_Email_ID__c = ac.Req_Owner_Email__c;
ac.status1__c = 'Rejected';
ac.MR_Status_Date__c = DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
}

system.debug('ac:'+ ac);
system.debug('Status:' + ac.status1__c);
system.debug('Priority:' + ac.Priority__c);
system.debug('Manager Email:' + ac.Manager_Email_ID__c);
system.debug('LR_Email__c :' + ac.LR_Email__c);
system.debug('R_Email__c :' + ac.R_Email__c );
system.debug('MR_Status1__c:' + ac.MR_Status1__c );

}

 


}


}

 

 

Test class: 

 

@isTest(seealldata=true)
private class TestTriggerStatusUpdate
{
static testMethod void myUnitTest()
{
Candidate_Mapping__c coa =new Candidate_Mapping__c();
coa.status1__c = 'Applied';
coa.Employer_s_Authorization__c = 'test';
coa.LR_Comments__c = 'test by lr';
coa.MR_Comments__c = 'test by MR';
coa.Requirement_Owner_Email__c = 'testOwner@preludesys.com';
coa.Manager_Email_ID__c = 'testOwner1@preludesys.com';
coa.R_Comments__c = '';
coa.LR_Status__c = 'Rejected';
coa.MR_Status__c = 'Rejected';
coa.LR_Status_Date__c = DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
coa.Submitted_to_Client__c = 'No';
coa.Submitted_to_Client_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
coa.Interview_Scheduled__c = 'No';
coa.Interview_Scheduled_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
coa.Interview_Accepted__c = 'No';
coa.Interview_Accepted_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
coa.Client_Interviewed__c = 'No';
coa.Client_Interview_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
coa.Client_Offered__c = 'No';
coa.Client_Offer_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
coa.Candidate_started__c = 'No';
coa.Comments__c = '';
coa.started_date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
coa.MR_Status1__c = 'None' ;
coa.LR_Email__c = 'test@prel.com';
// coa.status1__c = 'Rejected';
//coa.Manager_Email_ID__c = ac.Req_Owner_Email__c;

Candidate__c cc1=new Candidate__c();
cc1.Name='test';
cc1.Last_name__c='testone';
cc1.Current_city__c='chennai';
cc1.Current_state__c='Maine';
cc1.Gender__c='Male';
cc1.Employer_Name__c='ttt';
cc1.Email__c='test@gmail.com';
cc1.interview_contact_phone__c='Mobile Phone';
cc1.Contract_type__c='Corp to Corp';
cc1.visa_type__c='H1 B';
cc1.cost__c =100;
cc1.mobile_phone_no__c='9890043500';
cc1.employer_contact_email__c='test@gmail.com';
cc1.Employer_Contact_Name_Name__c='tt';
cc1.Employer_Mobile_No__c='9988009876';
cc1.Employer_Name__c = 'jos';
cc1.Employer_Work_Phone_No__c = '(425) 264-6771';
cc1.Followup_Date__c =date.today();
insert cc1;
coa.Candidate__c=cc1.id;
system.debug('candid:'+cc1.id);

Requirement__c rc1=new Requirement__c();
rc1.Name='SampathReq';
rc1.Job_Title__c='.net';
//rc1.Client__c ='abc company';
rc1.Duration__c='6';
rc1.No_Of_Resumes__c=2;
rc1.Min_Cost__c=100;
rc1.Max_Cost__c=200;
rc1.Rate__c=200;
rc1.Rate_Basis__c='Hourly';
rc1.Status__c='open';
rc1.Position_Type__c='Contract';
rc1.State__c='CA';
rc1.City__c='CA';
rc1.Est_Start_Date__c=date.today();
rc1.won__c = true;
//rc1.won_text__c ='yes';
insert rc1;
coa.Requirement__c=rc1.id;


insert coa;

}
static testMethod void myUnitTest1()
{

Candidate_Mapping__c coa =new Candidate_Mapping__c();
coa.status1__c = 'Applied';
coa.Employer_s_Authorization__c = 'test';
coa.LR_Comments__c = 'test by lr';
coa.MR_Comments__c = 'test by MR';
coa.Requirement_Owner_Email__c = 'testOwner@preludesys.com';
coa.Manager_Email_ID__c = 'testOwner1@preludesys.com';
coa.R_Comments__c = '';
coa.LR_Status__c = 'Approved';
coa.MR_Status__c = 'Approved';
coa.LR_Status_Date__c = DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
coa.MR_Status_Date__c = DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
coa.Submitted_to_Client__c = 'Yes';
coa.Submitted_to_Client_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
coa.Interview_Scheduled__c = 'Yes';
coa.Interview_Scheduled_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
coa.Interview_Accepted__c = 'Yes';
coa.Interview_Accepted_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
coa.Client_Interviewed__c = 'Yes';
coa.Client_Interview_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
// coa.status1__c = 'Client Interviewed';
coa.Client_Offered__c = 'Yes';
coa.Client_Offer_Date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');
// coa.status1__c = 'Client Offered';
coa.Candidate_started__c = 'Yes';
coa.Comments__c = '';
coa.MR_Status1__c = 'Approved' ;
coa.started_date__c= DateTime.now().format('MM-dd-yyyy hh:mm a z', 'America/Los_Angeles');

Candidate__c cc1=new Candidate__c();
cc1.Name='test';
cc1.Last_name__c='testone';
cc1.Current_city__c='chennai';
cc1.Current_state__c='Maine';
cc1.Gender__c='Male';
cc1.Employer_Name__c='ttt';
cc1.Email__c='test@gmail.com';
cc1.interview_contact_phone__c='Mobile Phone';
cc1.Contract_type__c='Corp to Corp';
cc1.visa_type__c='H1 B';
cc1.cost__c =100;
cc1.mobile_phone_no__c='9890043500';
cc1.employer_contact_email__c='test@gmail.com';
cc1.Employer_Contact_Name_Name__c='tt';
cc1.Employer_Mobile_No__c='9988009876';
cc1.Employer_Name__c = 'jos';
cc1.Employer_Work_Phone_No__c = '(425) 264-6771';
cc1.Followup_Date__c =date.today();
insert cc1;
coa.Candidate__c=cc1.id;
system.debug('candid:'+cc1.id);

Requirement__c rc1=new Requirement__c();
rc1.Name='SampathReq';
rc1.Job_Title__c='.net';
//rc1.Client__c ='abc company';
rc1.Duration__c='6';
rc1.No_Of_Resumes__c=2;
rc1.Min_Cost__c=100;
rc1.Max_Cost__c=200;
rc1.Rate__c=200;
rc1.Rate_Basis__c='Hourly';
rc1.Status__c='open';
rc1.Position_Type__c='Contract';
rc1.State__c='CA';
rc1.City__c='CA';
rc1.Est_Start_Date__c=date.today();
rc1.won__c = true;
//rc1.won_text__c ='yes';
insert rc1;
coa.Requirement__c=rc1.id;

User user = [Select Id from User limit 1] ; //where Email= 'gopalmr140313@gmail.com'];

EmailTemplate et = [Select Id, Subject, HtmlValue, Body from EmailTemplate where Name = 'Candidate Approval Email with link EZRE'];
List<ContentVersion> contentList = new List<ContentVersion>();

ContentWorkspace library = [SELECT id FROM ContentWorkspace LIMIT 1];
System.Debug('library.id:'+ library.id);
/* contentList = [Select Id, ContentDocumentId,VersionData from ContentVersion where FirstPublishLocationId = :library.id];
Blob strVersionData;
for(ContentVersion cversion :contentList)
{
strVersionData = cversion.VersionData;
System.Debug('strVersionData&colon;'+ strVersionData);
} */
ContentVersion contentVersionObj = new ContentVersion();
//contentVersionObj.ContentURL = 'http://www.google.com';
contentVersionObj.title = 'Google';//contentVersionObj.Enablement_Area__c = 'Acct Mgmt';
contentVersionObj.FirstPublishLocationId = library.id;
contentVersionObj.Candidate_Id__c = cc1.Id;
contentVersionObj.VersionData = Blob.valueOf('Ayangar') ;
contentVersionObj.PathOnclient = 'http://www.google.com';
contentVersionObj.Title ='Management Approach';
// contentVersionObj.ContentDocumentId = cv1.ContentDocumentId;

insert contentVersionObj;
System.Debug('ContentVersionObj:'+ contentVersionObj);

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('Sasikumar.Word');
efa.setBody(Blob.ValueOf(et.Body));
//efa.setBody(Blob.valueOf('Ayangar'));
//efa.setContentType('.word'); //
System.Debug('efa:' +efa);
String[] toAddresses = new string[]{'dummy@yahoo.com'};
mail.setTargetObjectId(user.Id);
mail.setToAddresses(toAddresses);
//mail.setplaintextBody('plainbody');

mail.setSenderDisplayName('Charan Tej');

mail.setTemplateId(et.Id);
// mail.setWhatId(c.Id);
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
mail.saveAsActivity = false;
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
//


insert coa;

Candidate_Mapping__c coa1 = [Select Id,Status1__c, LR_Status__c, MR_Status__c, MR_Status1__c from Candidate_Mapping__c where Candidate__c =: cc1.Id and Requirement__c =: rc1.id limit 1];
coa1.Status1__c = 'Candidate started';
// coa.LR_Status__c = 'Approved';
// coa.MR_Status__c = 'Rejected';
//coa1.Status1__c = 'LR Rejected';
update coa1;



}
}

 

 

 

 

 

 

 

MJ Kahn / OpFocusMJ Kahn / OpFocus

Unfortunately, there's way too much code here (and code that's dependent on custom objects for which we don't know the full definition) to realistically expect someone here to write out more complete unit tests for you. However, after you've run your unit tests, you can find out which lines haven't been covered by going to Setup | Develop | Apex Triggers, and clicking the link in the "Code Coverage" column. That will show you (in blue) the lines of code that have been covered, and (in red) the lines that haven't been covered. That should give you enough information to know how to enhance your unit tests to get more code covered.

jordi vosjordi vos

MJ Kahn's suggestion is the best advise you can get. Look at what lines are covered. I'm most certain that at least not all the if statements are covered.

 

Also, coverage should not be the goal(well, overcourse you need 75% to deploy). The goal should be to test all the diffrent outcomes and situations in your code, and assert those.if you have 10 IF statements, you should cover all 10 of those.

Also, what most people forget is to test what should happen when your code fails.