+ Start a Discussion
GM_91GM_91 

calling a list in testcoverage

hiii this is my test class....i just want to write a test coverage for this code.....but i got only 50% of code coverage...can anyone plsss...help me out....

 

public class MultipleStdntView1_AC
{
   public pagereference save()
   {
       return null;
    }
   String recordId;
   public MultipleStdntView1_AC()
   {
      recordId = system.currentpagereference().getparameters().get('id');
      c = [select Name,dept_email__c,dept_ID__c from department__c where id =: recordId];
      stdntlist = [Select Name,Email__c,phone__c,percentage__c from Student__c where department__c =: c.id];
    }
  
   Department__c c = new Department__c();
   public department__c getdepartmentDetails()
   {
     return c;
   }
   List<Student__c> stdntlist = new List<Student__c>();
   public List<Student__c> getstudentDetails()
   {
     return stdntlist;
   }
   public PageReference edit() 
    {
        return null;
    }

    public PageReference deleteRecords() 
    {
        return null;
    }
   
}

 

 

the red colored lines are the uncovered....

and also can you pls..tel me about "how to call a list & a loop?

Best Answer chosen by Admin (Salesforce Developers) 
asish1989asish1989
@isTest
private class TestControllerCustomerSpendGroup {

    static testMethod void CustomerSpendGroup() {
		department__c dept = new department__c();
		dept.Name = 'test';
		dept.dept_email__c = 'test@gmail.com';
		dept.dept_ID__c = 'test';
		insert dept;
		 ApexPages.currentPage().getParameters().put('Id', dept.Id);
		Student__c student = new Student__c();
		student. Name = 'test';
		Student.Email__c = 'test@gmail.com';
		Student.phone__c = '5678897671'
		Student.percentage__c = 34;
		Student.department__c = dept.id;
		insert Student;
		MultipleStdntView1_AC test = new MultipleStdntView1_AC();
		test.getstudentDetails();
		test.edit();
		test.deleteRecords();
		test.getdepartmentDetails();
		test.save();
	}
}	
	

 

All Answers

souvik9086souvik9086

Can you please post your test class code what you had written so far?

 

Thanks

vbsvbs

@GM - You will have to call the controller methods in your test class explicitly to simulate user actions. Try this:

<controllerVariable>.getStudentDetails();
system.assert(<controllerVariable>.edit() == null);
system.assert(<controllerVariable>.deleterecords() == null);
system.assert(<controllerVariable>.save() == null);

 

The assumption here is that - you have setup test data and the VFPage in the test class before calling the controller methods. Make sure you pass the right Id for the query parameters.

asish1989asish1989
@isTest
private class TestControllerCustomerSpendGroup {

    static testMethod void CustomerSpendGroup() {
		department__c dept = new department__c();
		dept.Name = 'test';
		dept.dept_email__c = 'test@gmail.com';
		dept.dept_ID__c = 'test';
		insert dept;
		 ApexPages.currentPage().getParameters().put('Id', dept.Id);
		Student__c student = new Student__c();
		student. Name = 'test';
		Student.Email__c = 'test@gmail.com';
		Student.phone__c = '5678897671'
		Student.percentage__c = 34;
		Student.department__c = dept.id;
		insert Student;
		MultipleStdntView1_AC test = new MultipleStdntView1_AC();
		test.getstudentDetails();
		test.edit();
		test.deleteRecords();
		test.getdepartmentDetails();
		test.save();
	}
}	
	

 

This was selected as the best answer
GM128GM128
@isTest
public class MultipleStdntView_TC
{
static testmethod void testLoadData()
{
Department__c dept = new department__c();
dept.Name = 'ECE';
insert dept;
Student__c s= new Student__c();
s.Name='Test';
S.Department__c = dept.id;
insert s;

system.currentpagereference().getparameters().put('id',dept.id);


MultipleStdntView1_AC mu=new MultipleStdntView1_AC();
mu.getdepartmentdetails();

/*MultipleStdntView1_AC.stdntlist sl= new MultipleStdntView1_AC.studentlist();
mu.getstudentdetails();*/
}
}
this is my test class i had written so far
GM_91GM_91

@isTest
public class MultipleStdntView_TC
{
static testmethod void testLoadData()
{
Department__c dept = new department__c();
dept.Name = 'ECE';
insert dept;
Student__c s= new Student__c();
s.Name='Test';
S.Department__c = dept.id;
insert s;

system.currentpagereference().getparameters().put('id',dept.id);


MultipleStdntView1_AC mu=new MultipleStdntView1_AC();
mu.getdepartmentdetails();
/*MultipleStdntView1_AC.stdntlist sl= new MultipleStdntView1_AC.studentlist();
mu.getstudentdetails();*/
}
}

this is my test class sir

GM_91GM_91
thanq so much anish sir...the code which u wrote is 100% covered...:)
GM_91GM_91
i hv an another doubt can u pls clear it....

how we can call a "for loop" in test coverage sir
asish1989asish1989

You mean how to cover a for loop in a test class?

For example .. how to cover that loop? 

is your question

listOfAccounts = [SELECT id Name , BillingCity  From Account WHERE BillingCity  = 'India'];

for(Account act: listOfAccounts){

// some processing

}

 

Ans--In your test class insert some account records whose BillingCity  = india;It will automatically cover that for loop.

 

 

 

GM_91GM_91

suppose this is my for loop how can i cover this?


for(Account ac:ob)

{

      inner class ic=new inner class();

      ic.sno=count++;

      ic.accountdetails=ac;

      innerlist.add(ic);

}

 

asish1989asish1989

Insert Account,

Add these line aslo  

OuterClassName.InnserClassname clsObj = new OuterClassName.InnserClassname();

 

It will automatically cover.