You need to sign in to do that
Don't have an account?
Frank Carter
problem with test class for search page
hi, I'm hoping someone can point me in the right direction.
I have a vf page to search child object related to Opportunity.
In input user have to insert two string Inputtext (owner and id opportunity).
In apex class there is a void method load data:
Apex class:
the test class:
The code coverage is 61% but i don't understand why it's like it doesn't see the content of the inputValue1 and inputValue2.
I'm hoping someone could have a look at my test class and let me know how I should go about passing these parameters.
any help would be appreciated
Thanks
I have a vf page to search child object related to Opportunity.
In input user have to insert two string Inputtext (owner and id opportunity).
In apex class there is a void method load data:
Apex class:
public class SearchWithWrapperSS { public Date StartDate {get;set;} public Date EndDate {get;set;} public String inputValue1 {get;set;} public String inputValue2 {get;set;} public List<WrapperClass> listBD {get;set;} public List<Billing_Detail__c> blst=new List<Billing_Detail__c>(); public void loadData() { if ((! String.isBlank(inputValue1))&&(String.isBlank(inputValue2))){ String Owner; List <User> userList = [Select id, Name from User where Email =: inputValue1]; if(! userList.isEmpty()){ Owner = userList[0].id; List <Billing_Detail__c> billingList = [Select Id,Account_Name__c,SF_Opportunity_Id__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,Billing_Competence_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c from Billing_Detail__c Where Billing_Detail__c IN (select id from Opportunity where OwnerId=:Owner)and Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate and Billing_Status__c !='Billed' Order by Monthly_Forecast__c ]; listBD = new List<WrapperClass>(); for(Billing_Detail__c cr : billingList ){ listBD.add(new WrapperClass (cr, false)); } } } else if ((! String.isBlank(inputValue1))&&(! String.isBlank(inputValue2))){ String Owner; List <User> userList = [Select id, Name from User where Email =: inputValue1]; if(! userList.isEmpty()){ Owner = userList[0].id; List <Billing_Detail__c> listBilling = [Select Id,Account_Name__c,SF_Opportunity_Id__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,Billing_Competence_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c from Billing_Detail__c Where Billing_Detail__c IN (select id from Opportunity where OwnerId=:Owner) and Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate and SF_Opportunity_Id__c=:inputValue2 and Billing_Status__c !='Billed' Order by Monthly_Forecast__c ]; listBD = new List<WrapperClass>(); for(Billing_Detail__c cr : listBilling){ listBD.add(new WrapperClass (cr, false)); } } } else if (! String.isBlank(inputValue2)){ listBD = new List<WrapperClass>(); for(Billing_Detail__c cr : [Select Id,Account_Name__c,SF_Opportunity_Id__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,Billing_Competence_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c from Billing_Detail__c Where Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate and SF_Opportunity_Id__c=:inputValue2 and Billing_Status__c !='Billed' Order by Monthly_Forecast__c ]){ listBD.add(new WrapperClass (cr, false)); } } else{ listBD = new List<WrapperClass>(); for(Billing_Detail__c cr : [Select Id,Account_Name__c,SF_Opportunity_Id__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,Billing_Competence_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c from Billing_Detail__c Where Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate and Billing_Status__c !='Billed' Order by Monthly_Forecast__c ]){ listBD.add(new WrapperClass (cr, false)); } } } public List<WrapperClass> getBilling() { if(listBD == null) { listBD = new List<WrapperClass>(); for(Billing_Detail__c cr : [Select Id,Account_Name__c,SF_Opportunity_Id__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,Billing_Competence_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c from Billing_Detail__c Where Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate and Billing_Status__c !='Billed' Order by Monthly_Forecast__c ]){ listBD.add(new WrapperClass (cr, false)); } } return listBD; } public PageReference changeStatus() { List<Billing_Detail__c> selectedBD = new List<Billing_Detail__c>(); for(WrapperClass bd: getBilling()){ Billing_Detail__c billlingDetail ; if(bd.check_box== true){ billlingDetail = new Billing_Detail__c(Id=bd.cs.Id); billlingDetail.Billing_Status__c= 'Authorized for Billing'; selectedBD.add(billlingDetail); } } update selectedBD; loadData(); return null; } public PageReference processSelected() { List<Billing_Detail__c> selectedBD = new List<Billing_Detail__c>(); for(WrapperClass bd: getBilling()){ Billing_Detail__c billlingDetail ; if(bd.check_box== true){ billlingDetail = new Billing_Detail__c(Id=bd.cs.Id); if (bd.cs.Monthly_Forecast__c.month()==12) { billlingDetail.Monthly_Forecast__c = Date.newinstance( bd.cs.Monthly_Forecast__c.year()+1 , 01 , bd.cs.Monthly_Forecast__c.day() ); billlingDetail.Billing_Competence_Forecast__c = billlingDetail.Monthly_Forecast__c; selectedBD.add(billlingDetail); } else{ billlingDetail.Monthly_Forecast__c = Date.newinstance( bd.cs.Monthly_Forecast__c.year() , bd.cs.Monthly_Forecast__c.month()+1, bd.cs.Monthly_Forecast__c.day() ); billlingDetail.Billing_Competence_Forecast__c = billlingDetail.Monthly_Forecast__c; selectedBD.add(billlingDetail); } } } update selectedBD; loadData(); return null; } //save public PageReference toSave() { List<Billing_Detail__c> selectedBD = new List<Billing_Detail__c>(); for(WrapperClass bd: getBilling()){ Billing_Detail__c billlingDetail ; billlingDetail = new Billing_Detail__c(Id=bd.cs.Id,Amount__c=bd.cs.Amount__c, Billing_Type__c=bd.cs.Billing_Type__c,Billing_Competence_Forecast__c=bd.cs.Billing_Competence_Forecast__c, Billing_Period__c=bd.cs.Billing_Period__c, Billing_Status__c=bd.cs.Billing_Status__c, Monthly_Forecast__c=bd.cs.Monthly_Forecast__c,End_of_Billing__c=bd.cs.End_of_Billing__c ); selectedBD.add(billlingDetail); } update selectedBD; loadData(); return null; } public PageReference toDelete() { List<Billing_Detail__c> selectedBD = new List<Billing_Detail__c>(); for(WrapperClass bd: getBilling()){ Billing_Detail__c billlingDetail ; if(bd.check_box== true){ billlingDetail = new Billing_Detail__c(Id=bd.cs.Id); selectedBD.add(billlingDetail); } } delete selectedBD; loadData(); return null; } public class WrapperClass { public Billing_Detail__c cs {get; set;} public Boolean check_box {get; set;} public WrapperClass(Billing_Detail__c c, Boolean check_box){ this.cs = c; this.check_box = false; } } }
the test class:
@isTest private class SearchWithWrapperSSTest { @isTest static void testController() { Test.startTest(); Id RecordTypeIdAccount = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Customer_user').getRecordTypeId();//get only macrostructure RT // create the object in input of the class Account a= new Account(); a.name='Account test t'; //a.RecordTypeId='012w0000000iROM'; a.Branch__c='Italy'; a.Status__c='Customer'; a.BillingCountry='Italy'; a.Public_Administration_Account__c='No'; insert a; Opportunity testOpportunity = new Opportunity( AccountID = a.Id, Name = 'Test Opportunity', StageName = 'Needs Analysis', CloseDate = date.today().addDays(15) ); insert testOpportunity; Billing_Detail__c c= new Billing_Detail__c(); c.Amount__c= 100; c.Billing_Type__c='FEE'; c.Billing_Period__c='monthly'; c.Billing_Status__c='Authorized for Billing'; c.Product_Line__c='Interactive Experience (iX)'; c.Product__c='Chat Delivery'; c.Monthly_Forecast__c=Date.newInstance(2018, 7, 9); c.End_of_Billing__c=Date.newInstance(2019, 7, 9); c.Billing_Detail__c=testOpportunity.Id; insert c; User u = new user(); u.LastName = 'Admin'; u.Email = 'test1@test.com'; u.Alias = 'Tcode'; u.Username = 'test123@test.com.testClass'; u.CommunityNickname = 'test121'; u.LocaleSidKey = 'en_US'; u.TimeZoneSidKey = 'GMT'; u.ProfileId = [Select id, name from Profile where name = 'System Administrator' OR name = 'Amministratore del sistema'].id; u.LanguageLocaleKey = 'en_US'; u.EmailEncodingKey = 'UTF-8'; u.IsActive = true; insert u; Date StartDate= Date.newInstance( 2018, 07,1); Date EndDate= Date.newInstance( 2019,10,18 ); String inputValue1= 'test1@test.com'; String inputValue2= testOpportunity.SFDC_Opportunity_Id__c; list<WrapperClassTest> wrp= new list<WrapperClassTest>(); ApexPages.StandardController sc = new ApexPages.StandardController(c); SearchWithWrapperSS x =new SearchWithWrapperSS(); x.loadData(); x.listBD[0].check_box=true; for(WrapperClassTest wTest :wrp) { wTest.check_box=true; } x.changeStatus(); x.processSelected(); x.toSave(); x.toDelete(); Test.stopTest(); } Public class WrapperClassTest{ public Billing_Detail__c cs {get;set;} public Boolean check_box {get;set;} public WrapperClassTest(Billing_Detail__c c, Boolean check_box) { this.cs = c; this.check_box = false; } public WrapperClassTest(Billing_Detail__c c) { this.cs = c; this.check_box = false; } } }
The code coverage is 61% but i don't understand why it's like it doesn't see the content of the inputValue1 and inputValue2.
I'm hoping someone could have a look at my test class and let me know how I should go about passing these parameters.
any help would be appreciated
Thanks
Can you try making SOQL query on Opportunity afer its created and then use Opportunity Id in your inputvalue 2. Additionally put System.debug log for inputValue2 after x.loadData();.Then check the status in the log. My assumption here is, there is something wrong with inputValue2.
Thanks,
Ashish Singh.
thanks for answering.
the inpuvalue2 is an autonumber (SF-Opty-2668) in opportunity and is overturned on billing details with a process builder. So inputvalue1 is taken from opportunity while inputvalue2 is take in billing details. I don't understand why it doesn't go into the second if.
thanks
Frank
You have to assign the values for the property inputValue1, inputValue2 in controller before calling the loadData method from testclass .
Please consider the below changes in your test class:
String inputValue1= 'test1@test.com';
String inputValue2= testOpportunity.SFDC_Opportunity_Id__c; // use the soql here to get the auto number field value [select SFDC_Opportunity_Id__c from Opportunity where Id =:testOpportunity.Id limit 1]
SearchWithWrapperSS x =new SearchWithWrapperSS();
x.inputValue1 = inputValue1;
x.inputValue2 = inputValue2;
x.loadData();
Similarly you have to pass the Startdate and Enddate to class.
Thanks,
Maharajan.C
I have modifed test class according to your specifications:
Now it enters in the second if correctly but from line 51 of the apex class on out the code is note covered, others methods are note executed. Code coverage now is 19%. :(
You have to write multi methods in test class also to enter if/else logics.
Create one more method in test class and add the existing code you have used before my above comment wit out inputValue1, inputValue2, Startdate and EndDate like below.
SearchWithWrapperSS x =new SearchWithWrapperSS();
x.loadData();
So similarly you can write multiple methods to execute the loaddata with different data to meet the if else logics in Apex Class.
Thanks,
Maharajan.C
Thanks for answering me.I tried to inser other method but testController and testController and testController4 go in error: list index out of buonds:0
the problem seems to be on the list of billing details on this
I think it can be this instruction
listBD.add(new WrapperClass (cr, false));
Thanks,
Frank
You have to use the system.runAs() inside the method. Because you are using opportunity owner Id in filter based on the user email. Now you running the test class so the system will automatically consider you as the owner for all inserted records in test class and below query will not return any data. because the owner email is you but it need the owner of user record you are inserting in test method.
select id from Opportunity where OwnerId=:Owner
So use the below syntax to run the method as specific user am using your testController4 method . And don't create wrappper in test class because those wrapper class will be automatically covered if you're billing records are found.
Understand the class properly then you can easily complete the test class.
Thanks,
Maharajan.C