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
vasu takasivasu takasi 

How to Write a test method to cover Wrapper Class

I wrote a test method to cover code for the class shown below.

But its not covering the wrapper class .

please direct me to test wrapper class.

 

public with sharing class sno_in_table
{

public list<wrap> getRecords()

{
integer sno=1;
list<wrap>emps=new list<wrap>();
list<employee__c>emp_records=[select name,phone__c from employee__c];

for(employee__c em:emp_records)
{

emps.add(new wrap(sno,em));
sno++;

}
return emps;
}

 

//wrapper class

 

public class wrap
{
public integer sno{get;set;}
public employee__c emp{get;set;}

public wrap(integer sn, employee__c e)
{
emp=e;
sno=sn;
}

}

public static testmethod void m1()
{
sno_in_table sno=new sno_in_table();
sno.getrecords();
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Rajesh SriramuluRajesh Sriramulu

Hi

 

Integer int=56;

employee__c emp = new employee__c()

// if possible insert fields in this

sno_in_table.wrap cmp = new sno_in_table.wrap(int,emp);

 

 

Let me know any issue and if it solves ur problem plz accept it solution.

All Answers

Rajesh SriramuluRajesh Sriramulu

Hi

 

U can create an object for wrapper class like

 

sno_in_table.wrap wp = new sno_in_table.wrap();

 

with that object u can call the methods which are under this wrapper class.

 

 

Let me know any issue and if it solves ur problem plz accept it solution.

BondicloudBondicloud

 

@isTest
    public static void  testsno_in_table() {

       employee__c obj = new employee__c();

             obj.name='ram';


obj.phone__c=123456789;

insert obj;

 

      sno_in_table sno=new sno_in_table();
       sno.getRecords();
    }

vasu takasivasu takasi

its showing error as

 

" Constructor not defined: [sno_in_table.wrap].<Constructor>()"

Rajesh SriramuluRajesh Sriramulu

Hi

 

Integer int=56;

employee__c emp = new employee__c()

// if possible insert fields in this

sno_in_table.wrap cmp = new sno_in_table.wrap(int,emp);

 

 

Let me know any issue and if it solves ur problem plz accept it solution.

This was selected as the best answer
NITIN BANGARNITIN BANGAR

Hi,

Did anyone found the solution for this

its giving error.

Ashish DeoAshish Deo
Same here facing same issue. Constructor not defined. what will be workaround for the same. Thanks!
Raviteja Vaddi 9Raviteja Vaddi 9
When constructor is not present then you need to give the empty method.
 Account_OrderedProjs_ListController.OppWrapper oo = new Account_OrderedProjs_ListController.OppWrapper();

Then you not get the error 'Constructor not defined'