You need to sign in to do that
Don't have an account?

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
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; } }
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]);
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.
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.
there are no triggers or workflows on both these parent(client discovery) and child (client site) objects.
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
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
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.
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.)
Specifically, check if all required fields of Client_Site__c instance have valid value.
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]