code coverage 100% in Apex class
write an apex class
public class CompanyClass
{
public class EmpClass
{
public Integer iEmpno;
public String iEmpName;
public Double iEmpSal;
public Integer iEmpDeptno;
//parameter constractor
public EmpClass(Integer iEmpno,String iEmpName,Double iEmpSal,Integer iEmpDeptno)
{
this.iEmpno=iEmpno;
this.iEmpName=iEmpName;
this.iEmpSal=iEmpSal;
this.iEmpDeptno=iEmpDeptno;
}
//default constractor
public EmpClass()
{
iEmpno=1;
iEmpName='varu';
iEmpSal=100;
iEmpDeptno=10;
}
//properties of EmpClass
public Integer Empno
{
get{ return iEmpno; }
set{ iEmpno=value; }
}
public String EmpName
{
get{ return iEmpName; }
set{ iEmpName=value; }
}
public Double EmpSal
{
get{ return iEmpSal; }
set{ iEmpSal=value; }
}
public Integer EmpDeptno
{
get{ return iEmpDeptno; }
set{ iEmpDeptno=value; }
}
}
//Emp list object
public List<EmpClass> emps=new List<EmpClass>();
//create Employees
public void createEmp()
{
emps.add(new EmpClass(1,'varun',1000,10));
emps.add(new EmpClass(2,'santhosh',2000,20));
emps.add(new EmpClass(3,'manoj',3000,30));
emps.add(new EmpClass(4,'mahesh',4000,30));
}
//search Emp
public EmpClass searchEmp(String empName,List<EmpClass> empList)
{
for(EmpClass e:empList)
{
if(e.iEmpName==empName)
return e;
}
return null;
}
//EmpClass
}write an apex test class
@isTest
public class CompanyClassTest
{
@isTest static void empDefaultConst()
{
CompanyClass.EmpClass obj=new CompanyClass.EmpClass();
System.assertEquals(1,obj.Empno);
System.assertEquals('varu',obj.EmpName);
System.assertEquals(100,obj.EmpSal);
System.assertEquals(10,obj.EmpDeptno);
}
@isTest static void empParaConst()
{
CompanyClass.EmpClass obj=new CompanyClass.EmpClass(50,'arya',3000,80);
System.assertEquals(50,obj.Empno);
System.assertEquals('arya',obj.EmpName);
System.assertEquals(3000,obj.EmpSal);
System.assertEquals(80,obj.EmpDeptno);
}
@isTest static void empCreateTest()
{
CompanyClass obj=new CompanyClass();
obj.createEmp();
Integer TotEmps=obj.Emps.size();
System.assertEquals(4,TotEmps);
}
@isTest static void empSearchTest()
{
CompanyClass obj=new CompanyClass();
obj.createEmp();
CompanyClass.EmpClass empObj=obj.searchEmp('varun',obj.emps);
System.assertEquals(1,empObj.Empno);
System.assertEquals('varun',empObj.EmpName);
System.assertEquals(1000,empObj.EmpSal);
System.assertEquals(10,empObj.EmpDeptno);
CompanyClass.EmpClass empObj1=obj.searchEmp('manoj',obj.emps);
System.assertEquals(3,empObj1.Empno);
System.assertEquals('manoj',empObj1.EmpName);
System.assertEquals(3000,empObj1.EmpSal);
System.assertEquals(30,empObj1.EmpDeptno);
CompanyClass.EmpClass empObj2=obj.searchEmp('santhosh',obj.emps);
System.assertEquals(2,empObj2.Empno);
System.assertEquals('santhosh',empObj2.EmpName);
System.assertEquals(2000,empObj2.EmpSal);
System.assertEquals(20,empObj2.EmpDeptno);
}
@isTest static void empSearchTest1()
{
CompanyClass obj=new CompanyClass();
obj.createEmp();
CompanyClass.EmpClass empObj3=obj.searchEmp('mahesh',obj.emps);
System.assert(empObj3!=null);
System.assertEquals(4,empObj3.Empno);
System.assertEquals('mahesh',empObj3.EmpName);
System.assertEquals(4000,empObj3.EmpSal);
System.assertEquals(30,empObj3.EmpDeptno);
}
@isTest static void empListTest()
{
CompanyClass obj=new CompanyClass();
System.assert(obj.emps!=null);
}
@isTest static void empProTest()
{
CompanyClass.EmpClass obj=new CompanyClass.EmpClass();
obj.Empno=22;
obj.EmpName='fff';
obj.EmpSal=222;
obj.EmpDeptno=33;
System.assertEquals(22,obj.Empno);
System.assertEquals('fff',obj.EmpName);
System.assertEquals(222,obj.EmpSal);
System.assertEquals(33,obj.EmpDeptno);
}
@isTest static void empListTest2()
{
CompanyClass.EmpClass obj=new CompanyClass.EmpClass();
CompanyClass obj1=new CompanyClass();
obj.Empno=22;
obj.EmpName='fff';
obj.EmpSal=222;
obj.EmpDeptno=33;
obj1.emps.add(obj);
System.assert(obj1.emps!=null);
System.assertEquals(1,obj1.emps.size());
}
}run the test
write an apex class
public class CompanyClass
{
public class EmpClass
{
public Integer iEmpno;
public String iEmpName;
public Double iEmpSal;
public Integer iEmpDeptno;
//parameter constractor
public EmpClass(Integer iEmpno,String iEmpName,Double iEmpSal,Integer iEmpDeptno)
{
this.iEmpno=iEmpno;
this.iEmpName=iEmpName;
this.iEmpSal=iEmpSal;
this.iEmpDeptno=iEmpDeptno;
}
//default constractor
public EmpClass()
{
iEmpno=1;
iEmpName='varu';
iEmpSal=100;
iEmpDeptno=10;
}
//properties of EmpClass
public Integer Empno
{
get{ return iEmpno; }
set{ iEmpno=value; }
}
public String EmpName
{
get{ return iEmpName; }
set{ iEmpName=value; }
}
public Double EmpSal
{
get{ return iEmpSal; }
set{ iEmpSal=value; }
}
public Integer EmpDeptno
{
get{ return iEmpDeptno; }
set{ iEmpDeptno=value; }
}
}
//Emp list object
public List<EmpClass> emps=new List<EmpClass>();
//create Employees
public void createEmp()
{
emps.add(new EmpClass(1,'varun',1000,10));
emps.add(new EmpClass(2,'santhosh',2000,20));
emps.add(new EmpClass(3,'manoj',3000,30));
emps.add(new EmpClass(4,'mahesh',4000,30));
}
//search Emp
public EmpClass searchEmp(String empName,List<EmpClass> empList)
{
for(EmpClass e:empList)
{
if(e.iEmpName==empName)
return e;
}
return null;
}
//EmpClass
}write an apex test class
@isTest
public class CompanyClassTest
{
@isTest static void empDefaultConst()
{
CompanyClass.EmpClass obj=new CompanyClass.EmpClass();
System.assertEquals(1,obj.Empno);
System.assertEquals('varu',obj.EmpName);
System.assertEquals(100,obj.EmpSal);
System.assertEquals(10,obj.EmpDeptno);
}
@isTest static void empParaConst()
{
CompanyClass.EmpClass obj=new CompanyClass.EmpClass(50,'arya',3000,80);
System.assertEquals(50,obj.Empno);
System.assertEquals('arya',obj.EmpName);
System.assertEquals(3000,obj.EmpSal);
System.assertEquals(80,obj.EmpDeptno);
}
@isTest static void empCreateTest()
{
CompanyClass obj=new CompanyClass();
obj.createEmp();
Integer TotEmps=obj.Emps.size();
System.assertEquals(4,TotEmps);
}
@isTest static void empSearchTest()
{
CompanyClass obj=new CompanyClass();
obj.createEmp();
CompanyClass.EmpClass empObj=obj.searchEmp('varun',obj.emps);
System.assertEquals(1,empObj.Empno);
System.assertEquals('varun',empObj.EmpName);
System.assertEquals(1000,empObj.EmpSal);
System.assertEquals(10,empObj.EmpDeptno);
CompanyClass.EmpClass empObj1=obj.searchEmp('manoj',obj.emps);
System.assertEquals(3,empObj1.Empno);
System.assertEquals('manoj',empObj1.EmpName);
System.assertEquals(3000,empObj1.EmpSal);
System.assertEquals(30,empObj1.EmpDeptno);
CompanyClass.EmpClass empObj2=obj.searchEmp('santhosh',obj.emps);
System.assertEquals(2,empObj2.Empno);
System.assertEquals('santhosh',empObj2.EmpName);
System.assertEquals(2000,empObj2.EmpSal);
System.assertEquals(20,empObj2.EmpDeptno);
}
@isTest static void empSearchTest1()
{
CompanyClass obj=new CompanyClass();
obj.createEmp();
CompanyClass.EmpClass empObj3=obj.searchEmp('mahesh',obj.emps);
System.assert(empObj3!=null);
System.assertEquals(4,empObj3.Empno);
System.assertEquals('mahesh',empObj3.EmpName);
System.assertEquals(4000,empObj3.EmpSal);
System.assertEquals(30,empObj3.EmpDeptno);
}
@isTest static void empListTest()
{
CompanyClass obj=new CompanyClass();
System.assert(obj.emps!=null);
}
@isTest static void empProTest()
{
CompanyClass.EmpClass obj=new CompanyClass.EmpClass();
obj.Empno=22;
obj.EmpName='fff';
obj.EmpSal=222;
obj.EmpDeptno=33;
System.assertEquals(22,obj.Empno);
System.assertEquals('fff',obj.EmpName);
System.assertEquals(222,obj.EmpSal);
System.assertEquals(33,obj.EmpDeptno);
}
@isTest static void empListTest2()
{
CompanyClass.EmpClass obj=new CompanyClass.EmpClass();
CompanyClass obj1=new CompanyClass();
obj.Empno=22;
obj.EmpName='fff';
obj.EmpSal=222;
obj.EmpDeptno=33;
obj1.emps.add(obj);
System.assert(obj1.emps!=null);
System.assertEquals(1,obj1.emps.size());
}
}run the test