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
DrebinDrebin 

Test class to insert a lead

Hi everyone,

I have this apex class for insert a new lead :
public class LeadClass
{
    public ContentVersion cont {get;set;}
    public String theValue {get;set;}
    public String company {get;set;}
    public String plate {get;set;}
    
    public LeadClass() {
        cont = new ContentVersion();
    }
    
    public PageReference NewLead(){
        List<User> updatedUsers = new List<User>();
        Decimal score = 0;
        List<User> myUsers = [SELECT Id, PhotoLead_Points__c
                              FROM User
                              WHERE Id=:userinfo.getuserId()];
        
        for (User myUser: myUsers) {
            myUser.PhotoLead_Points__c += 50;
            score = myUser.PhotoLead_Points__c;
            updatedUsers.add(myUser);
        }
        update updatedUsers;
        
        DateTime dt = DateTime.now();
        String formattedDt = dt.format('MM-dd-yyyy  hh:mm:ss');
        cont.PathOnClient = 'file' + Datetime.now() + '.jpg';
        cont.title = 'On the road ' + formattedDt;
        cont.Origin = 'H';
        upsert cont;
        
        Lead l = new Lead(PhotoLead_Image__c=cont.id, Company=company, Status='Open',LastName='On the road ' + formattedDt, Photo_location__c=theValue, Licence_plate__c=plate);
        insert l;
        
        PageReference pr = new PageReference('/'+l.id);
        return pr;
    }
    
    public PageReference doCancel()
    {
        return new PageReference('/00Q/o');
    }
    
    public PageReference game()
    {
        return new PageReference('/apex/Gamification');
    }
}
I tried to make a test class for this but i am still with a 0% code coverage :
@isTest 
public class LeadClassTest
{
    static testMethod void testMethod1() 
    {
        Lead newLead = new Lead() ;
        newLead.LastName = 'Swain';
        newLead.Company = 'BlueWave';
        newLead.Status = 'contacted';
        newLead.Photo_location__c = 'France';
        newLead.Licence_plate__c = 'GG-258-HH';
        insert newLead;
        
        User userToCreate = new User();
        
        // Do you recognize these fields?
        userToCreate.FirstName = 'Titi';
        userToCreate.LastName = 'Toto';
        userToCreate.Email = 'bdgdf@gmail.com';
        userToCreate.Username = 'fgnfy@gmail.com';
        userToCreate.Alias = 'huhu';
        userToCreate.ProfileId = '01er0010000iTqy';
        userToCreate.PhotoLead_Points__c = 50;
        insert userToCreate;
        
        userToCreate =[select IsActive from User  limit 1];
        userToCreate.IsActive = false;
        userToCreate.PhotoLead_Points__c = 100;
        update userToCreate;
        
    }
}
Any help would be appreaciated.
Thanks !
Best Answer chosen by Drebin
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest 
public class LeadClassTest
{
    static testMethod void testMethod1() 
    {
        Lead newLead = new Lead() ;
        newLead.LastName = 'Swain';
        newLead.Company = 'BlueWave';
        newLead.Status = 'contacted';
        newLead.Photo_location__c = 'France';
        newLead.Licence_plate__c = 'GG-258-HH';
        insert newLead;
        
		/*
		User userToCreate = new User();
        // Do you recognize these fields?
        userToCreate.FirstName = 'Titi';
        userToCreate.LastName = 'Toto';
        userToCreate.Email = 'bdgdf@gmail.com';
        userToCreate.Username = 'fgnfy@gmail.com';
        userToCreate.Alias = 'huhu';
        userToCreate.ProfileId = '01er0010000iTqy';
        userToCreate.PhotoLead_Points__c = 50;
        insert userToCreate;
        
        userToCreate =[select IsActive from User  limit 1];
        userToCreate.IsActive = false;
        userToCreate.PhotoLead_Points__c = 100;
        update userToCreate;
		*/
		
		
		Test.StartTest();
			LeadClass con = new LeadClass(); 
			con.doCancel();
			con.game();
			try
			{
				con.NewLead();
			}
			catch(Exception ee)
			{
			}
			
		Test.StopTes();
	
        
    }
}

Let us know if this will help you
 

All Answers

Vijaya AmarnathVijaya Amarnath
Hi Fred,


In test class you need to call the corrsponding class, other wise you'll not get the coverage for controllers...

try calling controller in test method like this.. It should be after inserting the test data in test method..

Test.StartTest();
LeadClass con = new LeadClass(); // intialize the controller
con.NewLead(); // call all methods from that controller
con.doCancel();
con.game();
Test.StopTes();


and also if the methods are static, then the way of calling methods will be diff..

ex:
controllerName.staticMethod();

In the same way for standard controller extentions it'll diff, please go through the docs.. You'll get clear idea how to call all type controllers in test methods..
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest 
public class LeadClassTest
{
    static testMethod void testMethod1() 
    {
        Lead newLead = new Lead() ;
        newLead.LastName = 'Swain';
        newLead.Company = 'BlueWave';
        newLead.Status = 'contacted';
        newLead.Photo_location__c = 'France';
        newLead.Licence_plate__c = 'GG-258-HH';
        insert newLead;
        
		/*
		User userToCreate = new User();
        // Do you recognize these fields?
        userToCreate.FirstName = 'Titi';
        userToCreate.LastName = 'Toto';
        userToCreate.Email = 'bdgdf@gmail.com';
        userToCreate.Username = 'fgnfy@gmail.com';
        userToCreate.Alias = 'huhu';
        userToCreate.ProfileId = '01er0010000iTqy';
        userToCreate.PhotoLead_Points__c = 50;
        insert userToCreate;
        
        userToCreate =[select IsActive from User  limit 1];
        userToCreate.IsActive = false;
        userToCreate.PhotoLead_Points__c = 100;
        update userToCreate;
		*/
		
		
		Test.StartTest();
			LeadClass con = new LeadClass(); 
			con.doCancel();
			con.game();
			try
			{
				con.NewLead();
			}
			catch(Exception ee)
			{
			}
			
		Test.StopTes();
	
        
    }
}

Let us know if this will help you
 
This was selected as the best answer
DrebinDrebin
Thanks to both of you !
I have 76% code coverage for this class now.