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
rajmulrajmul 

code coverage issue

                 

 

 

hi all i have created one class that dynamically fetch student records.when i created test class it is not covering all code.

 

 

 

 

 public with sharing class DynamicStudent  

{

 public String allstdnames { get; set; }

   public String selectedstudId { get; set; }

   public string Id{get;set;} 

  public student__c stdinfo{get;set;} 

  public list<SelectOption> getallstds()    {

 list<selectOption> options=new list<selectOption>(); 

   list<student__c> allstds=[select id,name from student__c limit 10]; 

   for(integer i=0;i<allstds.size();i++) 

   { 

   options.add(new selectOption(allstds[i].id,allstds[i].name)); 

   } 

   return options; 

   } 

    public void res() 

    {

     stdinfo=[select id,name from student__c where id=: selectedstudId ]; 

   }

 

-------------------------------------------------------

@isTest
private class testdynamicstudent{

static testMethod void myTest() {

DynamicStudent ds=new DynamicStudent();
list<selectoption> opt=new list<selectOption>();


opt.add(new selectoption('a0490000002khRH','select123'));

ds.getallstds();
ds.res();
}

 

}

 

 

how to pass the select option values in getallstds method

help  me

thanks  in advance

    

Thiyagarajan.SelvarajThiyagarajan.Selvaraj

Hi,

 

You have to create some test data for the student object in your test class.

 

Try this:

 

@isTest
public class testdynamicstudent{

public List<student__c> createStudents(){
List<student__c> studentList = new List<student__c>();
for (Integer i=0;i<10;i++){
studentList.add(new student__c(Name-'Test student '+i));
}
upsert studentList;
return [SELECT Id, Name FROM student__c];
}

static testMethod void test_getallstds(){
createStudents();
DynamicStudent ds = new DynamicStudent();
System.assertEquals(10, ds.getallstds().size());
}
}

 

Thanks,

 

Thiyagarajan Selvaraj