function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tanushree Singh 11Tanushree Singh 11 

Test class running successfully but giving 0% code coverage

I have a trigger as follows:

trigger LeadConvert on Lead (after update) {

  // no bulk processing; will only run from the UI
  if (Trigger.new.size() == 1) {

    if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {

      // if a new account was created
      if (Trigger.new[0].ConvertedAccountId != null) {

        // update the converted account with some text from the lead
        Account a = [Select a.Id, a.Initial_Payment__c, a.BPS__c,a.Monthly_Payment__c, a.X401_k_Plan_Type__c From Account a Where a.Id = :Trigger.new[0].ConvertedAccountId];
        if (a.X401_k_Plan_Type__c == 'Individual')
        {
            a.NumberOfEmployees = Integer.Valueof(Trigger.new[0].Number_of_Owners__c);
        }
        update a;

      }         

      }        

    }

  
}

It maps converted lead field to an account field.

Have written a test for the same which is giving 0% code coverage.

@isTest
private class LeadConvertTest {
@testSetup static void setup() {
        User users2=createUser();
        system.runAs(users2){
            Lead l1=new Lead();
            l1.LastName='test';
            l1.Number_of_Owners__c = 25;
            insert l1;
   
        }
    }
    
    @isTest
    static void updateAccount1(){
        user us2=[select Id from user where  Email ='xxx@ddd.com'];
        system.runAs(us2){
            Account a2=[select Id from Account limit 1];
            a2.X401_k_Plan_Type__c = 'Individual';
            update a2;
            
        }
        
    }
    
    
    private static user createUser(){
        User plannerUser2 = new User(
            ProfileId = [SELECT Id,name FROM Profile where name='System Administrator'].Id,
            LastName = 'last',
            Email = 'xxx@ddd.com',
            Username = 'xxx@ddd.com' + System.currentTimeMillis(),
            CompanyName = 'TEST1',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_GB',
            IsActive=true);
        database.insert(plannerUser2);
        return plannerUser2;
    }
    
}

I am new to development and any help woulf be graetly appreciated.
Best Answer chosen by Tanushree Singh 11
Shubham_KumarShubham_Kumar
Hi Tanushree

Your trigger runs on after update and on converted Lead but you have not converted lead in your trigger. 
Try the below code and let me know if you have any queries.
 
@isTest
private class LeadConvertTest {
	
    
    @isTest
    static void updateAccount1(){
        
		
		test.startTest();
		
		User users2=createUser();
        system.runAs(users2){
			Account ac = new Account();// Set all your necessary fields before inserting account
			ac.Name = 'test Account';
			ac.X401_k_Plan_Type__c = 'Individual';
			insert ac;
			
            Lead leadToConvert=new Lead();
            leadToConvert.LastName='test';
            leadToConvert.Number_of_Owners__c = 25;
            insert leadToConvert;
			
			//Converting the inserted Lead		
			database.leadConvert lc = new database.leadConvert();
			lc.setLeadId(leadToConvert.id);
			leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
			lc.setConvertedStatus(convertStatus.MasterLabel);
			lc.setAccountId(ac.Id);
			Database.LeadConvertResult lcr = Database.convertLead(lc);
			System.assert(lcr.isSuccess());
		    
		}
		test.stopTest();
        
	}
    
    
    private static user createUser(){
        User plannerUser2 = new User(
            ProfileId = [SELECT Id,name FROM Profile where name='System Administrator'].Id,
            LastName = 'last',
            Email = 'xxx@ddd.com',
            Username = 'xxx@ddd.com' + System.currentTimeMillis(),
            CompanyName = 'TEST1',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_GB',
		IsActive=true);
        database.insert(plannerUser2);
        return plannerUser2;
	}
    
}
P.S: mark this as the best answer if this helped you

Thanks
Shubham Kumar
 

All Answers

Sunil RathoreSunil Rathore
Hi,
Your trigger will execute on the update of the lead. You are updating only the account in the test class. You need to perform the update operation on the lead record.  Please refer the below code.
@isTest
private class LeadConvertTest {
@testSetup static void setup() {
	User users2=createUser();
	system.runAs(users2){
		Lead l1=new Lead();
		l1.LastName='test';
		l1.Number_of_Owners__c = 25;
		insert l1;
	}
	 l1.LastName='updated test';
	update  l1;
}
    
    @isTest
    static void updateAccount1(){
        user us2=[select Id from user where  Email ='xxx@ddd.com'];
        system.runAs(us2){
            Account a2=[select Id from Account limit 1];
            a2.X401_k_Plan_Type__c = 'Individual';
            update a2;
       }
        
    }
    
    
    private static user createUser(){
        User plannerUser2 = new User(
            ProfileId = [SELECT Id,name FROM Profile where name='System Administrator'].Id,
            LastName = 'last',
            Email = 'xxx@ddd.com',
            Username = 'xxx@ddd.com' + System.currentTimeMillis(),
            CompanyName = 'TEST1',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_GB',
            IsActive=true);
        database.insert(plannerUser2);
        return plannerUser2;
    }
    
}

Hope this will help you, if yes then mark it as best answer so it can also help others.

Many Thanks,
Sunil Rathore
 
Shubham_KumarShubham_Kumar
Hi Tanushree

Your trigger runs on after update and on converted Lead but you have not converted lead in your trigger. 
Try the below code and let me know if you have any queries.
 
@isTest
private class LeadConvertTest {
	
    
    @isTest
    static void updateAccount1(){
        
		
		test.startTest();
		
		User users2=createUser();
        system.runAs(users2){
			Account ac = new Account();// Set all your necessary fields before inserting account
			ac.Name = 'test Account';
			ac.X401_k_Plan_Type__c = 'Individual';
			insert ac;
			
            Lead leadToConvert=new Lead();
            leadToConvert.LastName='test';
            leadToConvert.Number_of_Owners__c = 25;
            insert leadToConvert;
			
			//Converting the inserted Lead		
			database.leadConvert lc = new database.leadConvert();
			lc.setLeadId(leadToConvert.id);
			leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
			lc.setConvertedStatus(convertStatus.MasterLabel);
			lc.setAccountId(ac.Id);
			Database.LeadConvertResult lcr = Database.convertLead(lc);
			System.assert(lcr.isSuccess());
		    
		}
		test.stopTest();
        
	}
    
    
    private static user createUser(){
        User plannerUser2 = new User(
            ProfileId = [SELECT Id,name FROM Profile where name='System Administrator'].Id,
            LastName = 'last',
            Email = 'xxx@ddd.com',
            Username = 'xxx@ddd.com' + System.currentTimeMillis(),
            CompanyName = 'TEST1',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_GB',
		IsActive=true);
        database.insert(plannerUser2);
        return plannerUser2;
	}
    
}
P.S: mark this as the best answer if this helped you

Thanks
Shubham Kumar
 
This was selected as the best answer
Tanushree Singh 11Tanushree Singh 11
Thanks a lot, Shubham! That worked!