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
bharath kuamarbharath kuamar 

Apex Test class

Hi,

 

 I have the following  class for which i have written the test class

public with sharing class DonorRegisterController {
 
    public DonorRegisterController () {
    }
    public String firstname {get; set;}
    public String LastName {get; set;}
    public String Company {get; set;}
    public String email {get; set;}
    public String address{get; set;}
       public String Status {get; set;}
    public String country{get; set;}
    public String mobilephone{get; set;}
    public String phone{get; set;}
    public String website{get; set;}
    public String fax{get; set;}
    
    public void registerUser() {

        Lead l = new Lead();
        l.firstname = firstname;
        l.LastName= lastname;
        l.Company= Company;
        l.Email = email;
          l.country= country;
                 l.Status = Status ;
         //l.Address=address;

         l.MobilePhone=mobilephone;
        l.Phone=phone;
        l.website=Website;
        l.Fax=fax;
        l.LeadSOurce='Web';
        insert l;
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Registered Succesfully !'));

    }

the test class for the above class is

static testmethod void DonorRegisterController()
    {

    DonorRegisterController registerdate = new DonorRegisterController();
        
       Lead l1 = new Lead();
      l1.firstname = 'test';
       l1.LastName = 'data';
        l1.Company = 'msat';
        l1.Email = 'abc@abc.com';
        l1.company = 'Msat';
        l1.MobilePhone='9895929516';
        l1.Phone='54545451';
        l1.website='www.salesforce.com';
        l1.Fax='45454545';
        l1.LeadSOurce='Web';
        l1.Status ='qualified';
      l1.country ='usa';
        insert l1;
        
      
         Lead l2 = new Lead();
      l2.firstname = 'test';
       l2.LastName = 'dates';
        l2.Company = 'Msat';
        l2.Email = 'abc@abc.com';
        l2.MobilePhone='9895929516';
        l2.Phone='54545451';
        l2.website='www.salesforce.com';
        l2.Fax='45454545';
        l2.Status ='qualified';
       l2.country ='india';
        l2.LeadSOurce='Web';
        insert l2 ;
          registerdate.registerUser();
    
    }
    
}
i have a validation rule on lead making that the coutnry field under lead should be mandatory, but when i fire the test classs it throws an exception that country is missing which i have called,can anyone help me with this case
Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference with 100% code coverage:

public with sharing class DonorRegisterController {

 

    public DonorRegisterController () {

    }

    public String firstname {get; set;}

    public String LastName {get; set;}

    public String Company {get; set;}

    public String email {get; set;}

    public String address{get; set;}

       public String Status {get; set;}

    public String country{get; set;}

    public String mobilephone{get; set;}

    public String phone{get; set;}

    public String website{get; set;}

    public String fax{get; set;}

   

    public void registerUser() {

 

        Lead l = new Lead();

        l.firstname = firstname;

        l.LastName= lastname;

        l.Company= Company;

        l.Email = email;

          l.country= country;

                 l.Status = Status ;

         //l.Address=address;

 

         l.MobilePhone=mobilephone;

        l.Phone=phone;

        l.website=Website;

        l.Fax=fax;

        l.LeadSOurce='Web';

        insert l;

        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Registered Succesfully !'));

 

    }

   

    static testmethod void DonorRegisterController()

    {

        DonorRegisterController dd1=new DonorRegisterController ();

         dd1.firstname='test1';

         dd1.LastName='test2';

         dd1.Company='testcompany';

         dd1.email='abc@test.com';

         dd1.address='test 1';

         dd1.Status='Open - Not Contacted';

         dd1.country='test1';

         dd1.mobilephone='1234567890';

         dd1.phone='1234567890';

         dd1.website='www.test.com';

         dd1.fax='1234567890';

         dd1.registerUser();

    }

   

    }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

@anilbathula@@anilbathula@

Hi Bharat 

 

Just Deactivate the validtion rule and run the test case.

After u push this class into production . 

There u can again write the validation rule.

 

Thanks

Anil.B

 

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference with 100% code coverage:

public with sharing class DonorRegisterController {

 

    public DonorRegisterController () {

    }

    public String firstname {get; set;}

    public String LastName {get; set;}

    public String Company {get; set;}

    public String email {get; set;}

    public String address{get; set;}

       public String Status {get; set;}

    public String country{get; set;}

    public String mobilephone{get; set;}

    public String phone{get; set;}

    public String website{get; set;}

    public String fax{get; set;}

   

    public void registerUser() {

 

        Lead l = new Lead();

        l.firstname = firstname;

        l.LastName= lastname;

        l.Company= Company;

        l.Email = email;

          l.country= country;

                 l.Status = Status ;

         //l.Address=address;

 

         l.MobilePhone=mobilephone;

        l.Phone=phone;

        l.website=Website;

        l.Fax=fax;

        l.LeadSOurce='Web';

        insert l;

        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Registered Succesfully !'));

 

    }

   

    static testmethod void DonorRegisterController()

    {

        DonorRegisterController dd1=new DonorRegisterController ();

         dd1.firstname='test1';

         dd1.LastName='test2';

         dd1.Company='testcompany';

         dd1.email='abc@test.com';

         dd1.address='test 1';

         dd1.Status='Open - Not Contacted';

         dd1.country='test1';

         dd1.mobilephone='1234567890';

         dd1.phone='1234567890';

         dd1.website='www.test.com';

         dd1.fax='1234567890';

         dd1.registerUser();

    }

   

    }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
bharath kuamarbharath kuamar

thats great , it worked