You need to sign in to do that
Don't have an account?

Test class code coverage
Hi all,
I written one trigger and test class, but code coverage is 33%. can any one help me
Trigger:-
---------
trigger LeadPreventUpdatePhone on Lead (before update) {
string UserName = userinfo.getUserName();
if(UserName.left(4) == 'sfdc'){
for (Lead Lead : System.Trigger.new) { // This line is not covered the code
Lead oldLead = Trigger.oldMap.get(Lead.ID); // This line is not covered the code
if(Lead.Phone != oldLead.Phone){ // This line is not covered the code
Lead.Phone = oldLead.Phone; // This line is not covered the code
}
}
}
Test class:-
--------------
@isTest
public class LeadPreventUpdatePhoneTestClass
{
@isTest static void LeadPreventUpdatePhone()
{
Profile p = [SELECT Id FROM Profile WHERE Name='SumT Support'];
User u = new User(Alias = 'Test', Email='Test@testorg.com',
EmailEncodingKey='UTF-8', LastName='Test', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='xsfdc@testorg.com');
System.runAs(u) {
// The following code runs as user 'u'
System.debug('Current User: ' + UserInfo.getUserName());
System.debug('Current Profile: ' + UserInfo.getProfileId());
Lead objLead=new Lead();
objLead.OwnerId=u.ID;
objLead.LastName='sampath';
objLead.Company='Testing';
objLead.Job_Function__c='VP1';
objLead.Department__c='HR1!';
objLead.Lead_Source_Category__c='Partner1';
objLead.Status='Open';
objLead.Email='sampath.kumar@octaware.com';
objLead.phone='515222632';
insert objLead;
Lead la = [select phone from Lead where id = :objLead.id limit 1];
System.debug('Original Lx:'+ la.phone);
System.assert(la.Phone == '515222632');
la.phone = '123456789';
update la;
Lead lb = [select phone from Lead where id = :objLead.id limit 1];
System.debug('Changed lb:'+ la.phone);
//COMMENT: then we assert that the field we updated equals the field it was updated from
System.assert(la.Phone == '123456789');
}
}
}
I written one trigger and test class, but code coverage is 33%. can any one help me
Trigger:-
---------
trigger LeadPreventUpdatePhone on Lead (before update) {
string UserName = userinfo.getUserName();
if(UserName.left(4) == 'sfdc'){
for (Lead Lead : System.Trigger.new) { // This line is not covered the code
Lead oldLead = Trigger.oldMap.get(Lead.ID); // This line is not covered the code
if(Lead.Phone != oldLead.Phone){ // This line is not covered the code
Lead.Phone = oldLead.Phone; // This line is not covered the code
}
}
}
Test class:-
--------------
@isTest
public class LeadPreventUpdatePhoneTestClass
{
@isTest static void LeadPreventUpdatePhone()
{
Profile p = [SELECT Id FROM Profile WHERE Name='SumT Support'];
User u = new User(Alias = 'Test', Email='Test@testorg.com',
EmailEncodingKey='UTF-8', LastName='Test', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='xsfdc@testorg.com');
System.runAs(u) {
// The following code runs as user 'u'
System.debug('Current User: ' + UserInfo.getUserName());
System.debug('Current Profile: ' + UserInfo.getProfileId());
Lead objLead=new Lead();
objLead.OwnerId=u.ID;
objLead.LastName='sampath';
objLead.Company='Testing';
objLead.Job_Function__c='VP1';
objLead.Department__c='HR1!';
objLead.Lead_Source_Category__c='Partner1';
objLead.Status='Open';
objLead.Email='sampath.kumar@octaware.com';
objLead.phone='515222632';
insert objLead;
Lead la = [select phone from Lead where id = :objLead.id limit 1];
System.debug('Original Lx:'+ la.phone);
System.assert(la.Phone == '515222632');
la.phone = '123456789';
update la;
Lead lb = [select phone from Lead where id = :objLead.id limit 1];
System.debug('Changed lb:'+ la.phone);
//COMMENT: then we assert that the field we updated equals the field it was updated from
System.assert(la.Phone == '123456789');
}
}
}
UserName.left in your test case will return xsfd and not SFDC.
Kindly update the username to start with SFDC.
All Answers
UserName.left in your test case will return xsfd and not SFDC.
Kindly update the username to start with SFDC.