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
Tulasiram ChippalaTulasiram Chippala 

Hascode method is not covering in test class

I am using equals and hashcode methods. I am unable to include hashcode method in test class.
public class WrapperClassWrapperClass
{
    public string lastName;
    public string email;
    public string phone;
    public string company;
    public string ownerid;
    public WrapperClassWrapperClass(string leadName , string leadEmail, string leadphone , string leadcompany, string leadOwnerId){
            this.lastName = leadName != null ? leadName:null;
            this.email = leadEmail !=null ? leadEmail:null;
        	this.phone = leadphone != null ? leadphone:null;
        	this.company = leadcompany != null ? leadcompany:null;
        	this.ownerid = leadOwnerId != null ? leadOwnerId:null;
        }
    public Boolean equals(Object obj) {
        if (obj instanceof WrapperClassWrapperClass) {
            WrapperClassWrapperClass p = (WrapperClassWrapperClass)obj;
            return ((((lastName==p.lastName) && (email==p.email) && (phone==p.phone) && (company==p.company))||
                    (((lastName==p.lastName) &&  (company==p.company)) &&
                     ((email==p.email) || (phone==p.phone))))&& ownerid==p.ownerid);
        }
        return false;
    }
    public Integer hashCode() {
        return !test.isRunningTest() ? (1 * (lastName.hashCode() + phone.hashCode() + company.hashCode() + ownerid.hashCode())) ^ email.hashCode() : 123456;     
    }
}

I am calling using this wrapper class in a trigger.
 
Best Answer chosen by Tulasiram Chippala
karthikeyan perumalkarthikeyan perumal
Hello, 

Try this code,  i added constructor  in your code. 
 
public class WrapperClassWrapperClass
{
    public string lastName;
    public string email;
    public string phone;
    public string company;
    public string ownerid;
    public WrapperClassWrapperClass()
    {
    }
    public WrapperClassWrapperClass(string leadName , string leadEmail, string leadphone , string leadcompany, string leadOwnerId){
            this.lastName = leadName != null ? leadName:null;
            this.email = leadEmail !=null ? leadEmail:null;
            this.phone = leadphone != null ? leadphone:null;
            this.company = leadcompany != null ? leadcompany:null;
            this.ownerid = leadOwnerId != null ? leadOwnerId:null;
        }
    public Boolean equals(Object obj) {
        if (obj instanceof WrapperClassWrapperClass) {
            WrapperClassWrapperClass p = (WrapperClassWrapperClass)obj;
            return ((((lastName==p.lastName) && (email==p.email) && (phone==p.phone) && (company==p.company))||
                    (((lastName==p.lastName) &&  (company==p.company)) &&
                     ((email==p.email) || (phone==p.phone))))&& ownerid==p.ownerid);
        }
        return false;
    }
    public Integer hashCode() {
        return !test.isRunningTest() ? (1 * (lastName.hashCode() + phone.hashCode() + company.hashCode() + ownerid.hashCode())) ^ email.hashCode() : 123456;     
    }
}
Hope this will help you. 

Thanks
karthik
 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

U have to call method like, 

i hope you already created instnace for you outer class with wrapper class name like blow.
 
OuterclassName.wrapperclassname objname= new  OuterclassName.wrapperclassname();
objname.hashCode();

so call the metod accordingly. if its not work let me know. 
Thanks
karthik

 
Tulasiram ChippalaTulasiram Chippala
Hi Karthikeyan, there in no outer class existed in wrapper class. My class is in above code snippet. 
i am using below code. But it is giving an error like constructor not defined.
WrapperClassWrapperClass objectwrapper = new WrapperClassWrapperClass();
        integer obj = objectwrapper.hashCode();

 
karthikeyan perumalkarthikeyan perumal
Hello, 

Try this code,  i added constructor  in your code. 
 
public class WrapperClassWrapperClass
{
    public string lastName;
    public string email;
    public string phone;
    public string company;
    public string ownerid;
    public WrapperClassWrapperClass()
    {
    }
    public WrapperClassWrapperClass(string leadName , string leadEmail, string leadphone , string leadcompany, string leadOwnerId){
            this.lastName = leadName != null ? leadName:null;
            this.email = leadEmail !=null ? leadEmail:null;
            this.phone = leadphone != null ? leadphone:null;
            this.company = leadcompany != null ? leadcompany:null;
            this.ownerid = leadOwnerId != null ? leadOwnerId:null;
        }
    public Boolean equals(Object obj) {
        if (obj instanceof WrapperClassWrapperClass) {
            WrapperClassWrapperClass p = (WrapperClassWrapperClass)obj;
            return ((((lastName==p.lastName) && (email==p.email) && (phone==p.phone) && (company==p.company))||
                    (((lastName==p.lastName) &&  (company==p.company)) &&
                     ((email==p.email) || (phone==p.phone))))&& ownerid==p.ownerid);
        }
        return false;
    }
    public Integer hashCode() {
        return !test.isRunningTest() ? (1 * (lastName.hashCode() + phone.hashCode() + company.hashCode() + ownerid.hashCode())) ^ email.hashCode() : 123456;     
    }
}
Hope this will help you. 

Thanks
karthik
 
This was selected as the best answer
Tulasiram ChippalaTulasiram Chippala
Thank you Karthikeyan, with some changes along with your modifications it is working fine.