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
troungertrounger 

creating a test case for wrapper class

Hi guys i am new to salesforce  anyone plese tell how to write a test class for fallowing apex class

 

public with sharing class ChildrenwithNoDonor2 {
public id conid;
public id conid1;
public ChildrenwithNoDonor2(ApexPages.StandardController controller) {
conid = Apexpages.currentpage().getparameters().get('cid');
conid1 = conid;
}


//Our collection of the class/wrapper objects cContact

public list<sstudent> studentlist{get; set;}

//Our collection of the class/wrapper objects cContact
public list<sstudent> getstudent(){
if(studentlist == null)
{
studentlist = new list<sstudent>();
for(student__c s: [select id,name,Contact__c,First_Name__c,Village__c,Available_Date__c,District__c,Close_Date1__c,Start_Date1__c from Student__c where contact__c = null ])
{
// As each contact is processed we create a new cContact object and add it to the contactList

studentlist.add(new sstudent(s));
}
}
return studentlist;
}
public PageReference AddtoTheDonar() {
//We create a new list of Contacts that we be populated only with Contacts if they are selected

list<student__c> selectedstudent = new list<student__c>();
//We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
for(sstudent sstu: getstudent()){
if(sstu.selected == true){
//sstu.contact__c = conid;
//upsert sstu;
selectedstudent.add(sstu.stu);

}
else{

}
}
// Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc

system.debug('these are the selected contacts');
for(student__c ss: selectedstudent){
// Integer size = ss.size();
// for(Integer i=0;i<ss.size();i++)
// {
// Student__C s = ss.get(i);
ss.Contact__c = conid;
update ss;

// }

}
// system.debug(ss);
// }

studentlist = null;
// we need this line if we performed a write operation because getContacts gets a fresh list now
return null;
}
public PageReference Back() {

PageReference secondPage = new PageReference ('/'+conid1);
secondPage.setRedirect(true);
return secondPage;
}
// This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value

public class sstudent{
public student__c stu {get; set;}
public Boolean selected {get; set;}
//This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false

public sstudent(student__c s){
stu = s;
selected = false;
}
}
}

Thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
troungertrounger

hi 

your code works about 70% coverage getting errors while creating pages 

 

i made few changes after that it will cover 95% remaining 5% error i am getting contact id dynamically so it's not covering that part here the code is

 

@istest
private class childwithnodonortest{
static testmethod void unittest(){
Contact objCon = new Contact();
objCon.firstName='unit';
objcon.LastName='test';
// Ur necessary Data
//objCon.AccountId = objAcc.Id;//If you are creating Account
insert objCon;
 
//Insert Dummy records of Student with their contact as Null

//List<Student__c> lststudInsert = new List<Student__c>();
//for(Int i=0;i<10;i++)
//{
Student__c objStudent = new Student__c();
//objStudent.Name= 'Test Stud Name'+i;
objStudent.First_Name__c = 'harsha';
//objStudent.contact__c = objcon.id;
//Put Necessary values for Student Data
//lststudInsert .add(objStudent);
//}
insert objStudent;

//Set conid to page
PageReference pageRef = Page.studentuserselect;
pageRef.getParameters().put('Id',objCon.id);
Test.setCurrentPage(pageRef);

ApexPages.StandardController controller = new ApexPages.StandardController(objCon);
   ChildrenwithNoDonor2 sc = new ChildrenwithNoDonor2(controller); 

//Create the object of controller
sc.studentlist = sc.getstudent();
//sc.studentlist[1].selected=True; 
sc.AddtoTheDonar();
sc.Back();
}}

All Answers

Ajay_SFDCAjay_SFDC

Hi there ,

 

//Firstly you have to get conid (I guess it is contact)


//Create the test record of Account and Associated contact

Account objAcc= new Account();
objAcc.Name ='TestAccoount';
insert objAcc;

 

Contact objCon = new Contact();
objCon.Name ='Test Contact';
// Ur necessary Data
objCon.AccountId = objAcc.Id;//If you are creating Account
insert objCon;

 

//Insert Dummy records of Student with their contact as Null


List<Student__c> lststudInsert = new List<Student__c>();
for(Int i=0;i<10;i++)
{
Student__c objStudent = new Student();
objStudent.Name= 'Test Stud Name'+i;
//Put Necessary values for Student Data
lststudInsert .add(objStudent);
}
insert lststudInsert;


//Set conid to page

PageReference pageRef = Page.YourPageNameHere;
pageRef.getParameters().put('Id',objCon.id);

Test.setCurrentPage(pageRef);

//Create the object of controller

ChildrenwithNoDonor2 objChildwithNoDonor2 = new ChildrenwithNoDonor2();

pageRef.AddtoTheDonar();
pageRef.Back();

 

 

Hit Kudos (click on start icon on left) if this provides you with useful information and mark it as a solution if it solves your problem.

 

Thanks and Regards

Ajay 

 

 

troungertrounger

hi 

your code works about 70% coverage getting errors while creating pages 

 

i made few changes after that it will cover 95% remaining 5% error i am getting contact id dynamically so it's not covering that part here the code is

 

@istest
private class childwithnodonortest{
static testmethod void unittest(){
Contact objCon = new Contact();
objCon.firstName='unit';
objcon.LastName='test';
// Ur necessary Data
//objCon.AccountId = objAcc.Id;//If you are creating Account
insert objCon;
 
//Insert Dummy records of Student with their contact as Null

//List<Student__c> lststudInsert = new List<Student__c>();
//for(Int i=0;i<10;i++)
//{
Student__c objStudent = new Student__c();
//objStudent.Name= 'Test Stud Name'+i;
objStudent.First_Name__c = 'harsha';
//objStudent.contact__c = objcon.id;
//Put Necessary values for Student Data
//lststudInsert .add(objStudent);
//}
insert objStudent;

//Set conid to page
PageReference pageRef = Page.studentuserselect;
pageRef.getParameters().put('Id',objCon.id);
Test.setCurrentPage(pageRef);

ApexPages.StandardController controller = new ApexPages.StandardController(objCon);
   ChildrenwithNoDonor2 sc = new ChildrenwithNoDonor2(controller); 

//Create the object of controller
sc.studentlist = sc.getstudent();
//sc.studentlist[1].selected=True; 
sc.AddtoTheDonar();
sc.Back();
}}

This was selected as the best answer
Ajay_SFDCAjay_SFDC

Hi there,

 

Great work !!  

I just given you the approach that you have to follow for wrtiting the test code of the mentioned class as I dont know what exactly your code is doing .

 

Thanks & Regards

Ajay

 

Hit Kudos (click on start icon on left) if that code provides you with useful information and mark it as a solution if it solves your problem.