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
SF7SF7 

Need help in Writing a test Class for a Wrapper Class

hi ,
I have a Visual force for Creating a One Parent (Client_Discovery__c)and multiple Child (Client Site__c) records at a time . I am using a Standard Controller. I have a Wrapper class and need some help on the Test class.

Account - Opportunity - Client Discovery - Client Site 
 
public class AddClientSite {
    ApexPages.StandardController sc;
    public Client_Discovery__c acct{get;set;}
    public Integer marker=2;
    public Integer selectedClientSite{get;set;}
    public List<WrapperClass> lClientSites{get;set;}
    public AddClientSite(ApexPages.StandardController controller) {
        this.acct = (Client_Discovery__c)controller.getRecord();
        sc=controller;
        lClientSites=new List<WrapperClass>();
        Client_Site__c c=new Client_Site__c();
        lClientSites.add(new WrapperClass(c,1));
    }
    public PageReference deleteClientSite(){
        Integer x=-1;
        for(WrapperClass wc:lClientSites){
            x++;
            if(wc.counter==selectedClientSite){
                System.debug('-->selected ClientSite:'+selectedClientSite+'  position:'+x);
                break;
            }
        }
        lClientSites.remove(x);
        return null;
    }
    public PageReference saveClientDiscovery(){
        Database.SaveResult sr = Database.insert(acct, false);
        Id idey=sr.getId();
        List<Client_Site__c> ClientSiteList=new List<Client_Site__c>();
        for(WrapperClass wc:lClientSites){
            Client_Site__c c=new Client_Site__c();
        c.Client_Site_Name__c=wc.c.Client_Site_Name__c;
        c.City__c=wc.c.City__c;
        c.State__c=wc.c.State__c;
        c.Head_count__c=wc.c.Head_count__c;
        c.Number_of_Shifts__c=wc.c.Number_of_Shifts__c;
        c.Job_Titles__c=wc.c.Job_Titles__c;
        c.Number_of_On_Site_Managers__c=wc.c.Number_of_On_Site_Managers__c;
        c.Union_or_Non_Union__c=wc.c.Union_or_Non_Union__c;
        c.Number_of_Team_Leads__c=wc.c.Number_of_Team_Leads__c;
        c.Supplier_Name__c=wc.c.Supplier_Name__c;
        c.Skill_Type__c=wc.c.Skill_Type__c;
        c.Will_Manpower_Manage_This_Supplier_del__c=wc.c.Will_Manpower_Manage_This_Supplier_del__c;
        c.Is_This_a_New_or_Incumbent_Supplier__c=wc.c.Is_This_a_New_or_Incumbent_Supplier__c;
        
        c.Client_Discovery__c=idey;
        ClientSiteList.add(c);
        }
        insert ClientSiteList;
        
         return(new PageReference('/'+sr.id).setRedirect(True));       
       // return new PageReference('/' + Schema.getGlobalDescribe().get('Client_Discovery__c').getDescribe().getKeyPrefix() + '/o');

    }
    public PageReference addAClientSite(){
        
        Client_Site__c c=new Client_Site__c();
        lClientSites.add(new WrapperClass(c,marker));        
        marker=marker+1;
        return null;
    }
    public class WrapperClass{
        public Integer counter{get;set;}
        public Client_Site__c c{get;set;}
        public WrapperClass(Client_Site__c cntc,Integer i){
            this.c=cntc;
            this.counter=i;
        }
    }
}
Right now its 0% Covered . 
@isTest
private class AddClientSiteTest {

public static testMethod void AddClientSite() {
                  
        Account a = new Account(name='TestOverrideClient one',BillingStreet='TestOverrideStone', Client_Short_Name__c ='ulttiCL',
                Phone='123454321',BillingState='ca', BillingPostalCode='95444', BillingCountry='US', BillingCity='TestOverrideOneCity', Ultimate_Parent__c = true);
        insert a;
        Opportunity o = new Opportunity(AccountId=a.Id,Name='testoppfornamechange',Solution_Type__c='sol1',
                Opportunity_Short_Description__c='test desc',Opportunity_Type__c='Parent',
                StageName='Plan & Engage', CloseDate=System.today()+2);
        Insert o;        
                
        Client_Discovery__c CD = new Client_Discovery__c (Opportunity__c=o.id) ;
        Insert CD;
        
        Client_Site__c CS = new Client_Site__c(Client_Discovery__c = CD.id);
        Insert CS;
              
    }
}


 
David ZhuDavid Zhu
You can use the following snippet as reference to append to the end of your code at line 19.
I didnot verify the syntax, but it should only need minimum change. replace the bold characters with your expected value.
========================================================
AddClientSite acs = new AddClientSite(CD);

system.assertequals(1,acs.lclientSites.size());  //assess constructor code

system.assertequals(null,acs.deleteclientSite()); //assert deletclientSite() method
system.assertequals(0,acs.licentsites.size());  

system.asertequals(null,acs.addaclientsite());//assert addaclientsite() method
system.asertequals(1,acs.licentsites.size()); 

system.assertequals(expeceddata,acs.saveClientDiscovery().geturl()); //assert saveClientDiscovery
system.assertequals(expectedrecordsnumberinserted,[select count() from client_site__c]); 
SF7SF7
@david Zhu

Thanks for the reply
I added the peice and there is one Small problem when i run the class there is one error . Bolded Line at the bottom. 
@isTest
private class AddClientSiteTest {

public static testMethod void AddClientSite() {
                  
        Account a = new Account(name='TestOverrideClient one',BillingStreet='TestOverrideStone', Client_Short_Name__c ='ulttiCL',
                Phone='123454321',BillingState='ca', BillingPostalCode='95444', BillingCountry='US', BillingCity='TestOverrideOneCity', Ultimate_Parent__c = true);
        insert a;
        Opportunity o = new Opportunity(AccountId=a.Id,Name='testoppfornamechange',Solution_Type__c='sol1',
                Opportunity_Short_Description__c='test desc',Opportunity_Type__c='Parent',
                StageName='Plan & Engage', CloseDate=System.today()+2);
        Insert o;        
                
        Client_Discovery__c CD = new Client_Discovery__c (Opportunity__c=o.id,
        
        What_services_do_you_anticipate_the_clie__c ='Test',
        What_SLAs_does_the_client_want_to_use_to__c ='Test',
        What_type_of_supplier_would_Manpower_be__c ='Test'
        
        ) ;
        Insert CD;
        
        Client_Site__c CS = new Client_Site__c(Client_Discovery__c = CD.id,
        City__c = 'test',
        State__c = 'test',
        Head_count__c = 1,
        Number_of_Shifts__c =1,
        Job_Titles__c ='test',
        Number_of_On_Site_Managers__c =1,
        Union_or_Non_Union__c ='Yes' ,
        Number_of_Team_Leads__c =1,
        Supplier_Name__c ='Test',
        Skill_Type__c ='yes',
        Will_Manpower_Manage_This_Supplier_del__c ='yes',
        Is_This_a_New_or_Incumbent_Supplier__c ='Yes'
        );
        Insert CS;
        
AddClientSite acs = new AddClientSite(new ApexPages.StandardController(CD));

system.assertequals(1,acs.lclientSites.size());  //assess constructor code

system.assertequals(null,acs.deleteclientSite()); //assert deletclientSite() method
system.assertequals(0,acs.lClientsites.size());  

system.assertequals(null,acs.addAClientSite());      //assert addaclientsite() method
system.assertequals(1,acs.lClientsites.size()); 

 
system.assertequals('Apex/ClientDiscoveryView/CD.ID',acs.saveAccount().geturl());            //assert saveClientDiscovery
system.assertequals(1,[select count() from client_site__c]);        
    }
}


System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Client_Discovery__c]: [Client_Discovery__c]

I am not missing any Required Fields and i tried having everythinga and it doesnt work. Any idea  what am i doing wrong.

Thanks
Akhil.
Niket SFNiket SF
Just check is there any trigger or any workflow is firing before inserting the "Client_Site__c" record ?

 
SF7SF7
@niket
there are no triggers or workflows on both these parent(client discovery) and child (client site) objects. 
David ZhuDavid Zhu
Hi Akhil,
Why would you use acs.saveAccount() in line 59? Is it supposed to use saveClientDiscovery? I don't see saveAccount() method in your code snippet.

David
SF7SF7
@david Zhu

I am using the same Name SaveAccount in both places in my Org. To just be clear i changed it here to be Saveclient DIsocvery
David ZhuDavid Zhu
I would suggest do it through UI to verify any required fields.
SF7SF7
HI David,

I added each and every field and still no luck . Do you think it is because i am creating both Parent and Child records at once is the Pb.
@isTest
private class AddClientSiteTest {

public static testMethod void AddClientSite() {
                  
        Account a = new Account(name='TestOverrideClient one',BillingStreet='TestOverrideStone', Client_Short_Name__c ='ulttiCL',
                Phone='123454321',BillingState='ca', BillingPostalCode='95444', BillingCountry='US', BillingCity='TestOverrideOneCity', Ultimate_Parent__c = true);
        insert a;
        Opportunity o = new Opportunity(AccountId=a.Id,Name='testoppfornamechange',Solution_Type__c='sol1',
                Opportunity_Short_Description__c='test desc',Opportunity_Type__c='Parent',
                StageName='Plan & Engage', CloseDate=System.today()+2);
        Insert o;        
                
        Client_Discovery__c CD = new Client_Discovery__c (Opportunity__c=o.id,
        What_is_the_client_s_purchase_decision_m__c = 'Test',
        Does_the_client_have_any_limitations_on__c  ='Test',
        Does_the_client_require_us_to_perform_se__c ='Test',
        Do_associates_need_to_be_transitioned_to__c ='Test',
        Does_the_client_allow_us_to_utilize_sub__c ='Test',
        Does_the_client_plan_to_maintain_current__c ='Test',
        Does_the_client_require_us_to_use_divers__c ='Test',
        Does_the_client_want_to_receive_reportin__c ='Test',
        Does_the_client_want_to_use_technology_t__c ='Test',
        Do_you_anticipate_any_other_ManpowerGrou__c ='Test',
        Has_the_client_requested_specific_paymen__c ='Test',
        How_does_the_client_manage_labor_demand__c ='Test',
        How_does_the_client_make_purchase_decisi__c ='Test',
        How_does_the_client_want_to_order_talent__c ='Test',
        How_does_the_client_want_to_receive_invo__c ='Test',
        How_does_the_client_want_to_receive_re__c ='Test',
        How_many_suppliers_does_the_client_expec__c ='Test',
        How_often_does_the_client_want_to_receiv__c ='Test',
        How_will_the_client_be_tracking_associat__c ='Test',
        If_Custom_metrics_are_needed_specify__c ='Test',
        If_Global_specify_the_countries_here__c ='Test',
        IF_Other_Client_requirement_4_Associates__c ='Test',
        If_VMS_or_Other_specify_the_type_of_sys__c ='Test',
        Is_the_client_a_candidate_for_Analytics__c ='Test',
        Is_the_Client_requesting_On_Site_Manag__c ='Test',
        List_limitations_here__c ='Test',
        List_Other_client_needs_here__c ='Test',
        List_Other_compliance_and_audit_requir__c ='Test',
        List_other_payment_terms_here__c ='Test',
        List_Other_technology_needs_here__c ='Test',
        List_the_method_with_which_the_client_wo__c ='Test',
        Other_Client_requirement_for_Associates__c ='Test',
        Other_MPG_Services_if_any__c ='Test',
        Other_MPG_Services_that_will_be_needed__c   ='Test', 
        Other_services_you_anticipate_for_client__c ='Test',
        What_are_reporting_metrics_does_the_clie__c ='Test',
        What_are_the_client_s_pain_points__c ='Test',
        What_are_the_client_s_requirements_for_a__c ='Test',
        What_is_the_anticipated_go_live_date__c =System.today()+2,
        What_is_the_anticipated_implmentation_st__c =System.today()+2,
        What_is_the_Client_s_invoice_frequency__c ='Test',
        What_is_the_frequency_of_the_client_s__c ='Test',
        What_is_the_geographic_scope_of_the_oppo__c ='Test',
        What_services_do_you_anticipate_the_clie__c ='Test',
        What_SLAs_does_the_client_want_to_use_to__c ='Test',
        What_type_of_supplier_would_Manpower_be__c ='Test'
        
        ) ;
        Insert CD;
        
        Client_Site__c CS = new Client_Site__c(Client_Discovery__c = CD.id,
        City__c = 'test',
        State__c = 'test',
        Head_count__c = 1,
        Number_of_Shifts__c =1,
        Job_Titles__c ='test',
        Number_of_On_Site_Managers__c =1,
        Union_or_Non_Union__c ='Yes' ,
        Number_of_Team_Leads__c =1,
        Supplier_Name__c ='Test',
        Skill_Type__c ='yes',
        Will_Manpower_Manage_This_Supplier_del__c ='yes',
        Is_This_a_New_or_Incumbent_Supplier__c ='Yes'
        );
        Insert CS;
        
AddClientSite acs = new AddClientSite(new ApexPages.StandardController(CD));

system.assertequals(1,acs.lclientSites.size());  //assess constructor code

system.assertequals(null,acs.deleteclientSite()); //assert deletclientSite() method
system.assertequals(0,acs.lClientsites.size());  

system.assertequals(null,acs.addAClientSite());      //assert addaclientsite() method
system.assertequals(1,acs.lClientsites.size()); 

 
system.assertequals('Client_Discovery__c.id',acs.saveAccount().geturl());  
//String assertURL = acs.saveAccount().getUrl();

//system.assertEquals('Apex/ClientDiscoveryView',assertURL);          //assert saveClientDiscovery
system.assertequals(1,[select count() from client_site__c]);        
    }
}

 
David ZhuDavid Zhu
I don't think that is the problem.
You need to verify four DMLs one by one through UI using the data in your test method.  (Account, Opportunity, Client_Discovery__c, and Client_Site__c.)
 
David ZhuDavid Zhu
You need to check implementation in  saveClientDiscovery (or saveaccount()) method. Issue is in your test mothed.
Specifically, check if all required fields of Client_Site__c instance have valid value.
SF7SF7
@David 



I added each and every required field on the Object to the test class and made sure there is no Required Field at all on the Client Disovery and Client Site object . And even in the VF page i have not specified any required Field , And below is the Debug log and when i go through it , it says i am missing requied field on VF page . And the field what i dont have on the VF page is the Client Disocvery (Look Up) Field  for Client Site which will be only created upon save as i am creating both parent and Child at once. 

11:41:28.595 (5595618332)|DML_BEGIN|[49]|Op:Insert|Type:Client_Site__c|Rows:1 11:41:28.601 (5601294369)|DML_END|[49]

11:41:28.601 (5601391721)|VF_PAGE_MESSAGE|Required fields are missing: [Client_Discovery__c]

11:41:28.601 (5601459183)|EXCEPTION_THROWN|[49]|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Client_Discovery__c]: [Client_Discovery__c]

11:41:28.602 (5602113580)|METHOD_EXIT|[95]|01p180000004No3|AddClientSite.saveAccount()

11:41:28.602 (5602170741)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Client_Discovery__c]: [Client_Discovery__c] Class.AddClientSite.saveAccount: line 49, column 1 Class.AddClientSiteTest.AddClientSite: line 95, column 1

11:41:28.602 (5602182742)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Client_Discovery__c]: [Client_Discovery__c]