+ Start a Discussion
Guru 91Guru 91 

Need Test class Help?

Hi, 
Please help me with test class


public   class OV_Bean implements Comparable {
  
    public String objId;
    public Long score;   
    
    public OV_Bean(String n, Long p) {     
        this.objId = n;
        this.score = p;
    }
    public  Integer compareTo(Object compareTo) {
        OV_Bean compareToEmp = (OV_Bean)compareTo;
        if (score > compareToEmp.score) return -1;
        if (score < compareToEmp.score) return 1;
        return 0;        
    }
}

Thanks
Best Answer chosen by Guru 91
Steven NsubugaSteven Nsubuga
@isTest
private class OV_BeanTest {
  
    @isTest
    static void test() {
       
        OV_Bean ovBean1 = new OV_Bean ('test', 34);
        OV_Bean ovBean2 = new OV_Bean ('tst', 35);
        OV_Bean ovBean3 = new OV_Bean ('tested', 34);
        OV_Bean ovBean4 = new OV_Bean ('testee', 37);
        List<OV_Bean> beans = new List<OV_Bean>();
        beans.add(ovBean3);
        beans.add(ovBean2);
        beans.add(ovBean1);
        beans.add(ovBean4);
        beans.sort();
        System.assert(beans[0].score == 37);
    }
}

 

All Answers

Steven NsubugaSteven Nsubuga
@isTest
private class OV_BeanTest {
  
    @isTest
    static void test() {
       
        OV_Bean ovBean1 = new OV_Bean ('test', 34);
        OV_Bean ovBean2 = new OV_Bean ('tst', 35);
        OV_Bean ovBean3 = new OV_Bean ('tested', 34);
        OV_Bean ovBean4 = new OV_Bean ('testee', 37);
        List<OV_Bean> beans = new List<OV_Bean>();
        beans.add(ovBean3);
        beans.add(ovBean2);
        beans.add(ovBean1);
        beans.add(ovBean4);
        beans.sort();
        System.assert(beans[0].score == 37);
    }
}

 
This was selected as the best answer
Raj VakatiRaj Vakati
This is the approach to for Comparable test classes
@isTest
private class OV_BeanTest {
    
    @isTest
    static void test() {
        
        OV_Bean ovBean1 = new OV_Bean ('test', 34);
        OV_Bean ovBean2 = new OV_Bean ('tst', 34);
        OV_Bean ovBean3 = new OV_Bean ('tested', 34);
        OV_Bean ovBean4 = new OV_Bean ('testee', 37);
        ovBean4.compareTo(ovBean3);  
        ovBean3.compareTo(ovBean4); 
        ovBean1.compareTo(ovBean2);
    }
}

 
Guru 91Guru 91
Thanks Raj