You need to sign in to do that
Don't have an account?
Renuka Sharma
how to use best pratices for test classes for visualforce controller
how to use best pratice for test classes for below code
my class
@isTest Private class TestProductSearchpopupController { @isTest static void testMethod1() { Account acc = new Account (name='Pencil'); insert acc; Opportunity opp= new Opportunity (); opp.name= 'Testopp1'; Opp.Accountid= acc.id; opp.CloseDate= date.today(); opp.StageName= 'Qualification'; insert opp; List <Quotes__c> lstquote = new List<Quotes__c>(); Quotes__c testque = new Quotes__c(); testque.Name = 'Test PriceBookEntry'; testque.OpportunityId__c = opp.id; testque.AccountId__c = acc.id; lstquote.add(testque); List <Product2__c> lstproduct = new List<Product2__c>(); Product2__c testProduct = new Product2__c(); testProduct.Name='Test product'; lstproduct.add(testProduct); List <PricebookEntry__c> lstpbe = new List<PricebookEntry__c>(); PricebookEntry__c testpbe = new PricebookEntry__c(); testpbe.Name='Test PriceBookEntry'; lstpbe.add(testpbe); List <QuoteLineitem__c> lstqli = new List<QuoteLineitem__c>(); QuoteLineitem__c testqli = new QuoteLineitem__c(); testqli.QuotesId__c = testque.id; lstqli.add(testqli); Test.startTest(); ApexPages.StandardController sc = new ApexPages.StandardController(lstquote[0]); ProductSearchPopupController psp = new ProductSearchPopupController(sc); psp.ProceedWithSelectedToNextPage(); psp.processSelected(); psp.runQuery(); psp.saveproduct(); Test.stopTest(); } }
my class
public class ProductSearchPopupController { public String query {get; set;} public List<PricebookEntry__c> products {get; set;} public List<wrapProduct> wrapProductList {get; set;} public List<PricebookEntry__c> selectedProduct{get;set;} public List<QuoteLineitem__c> quoteLineList{get;set;} public List<wrapProduct> selectedWrapperList{get;set;} public Boolean normalList{get;set;} public Boolean selectedList{get;set;} public Boolean block{get;set;} public Boolean block1{get;set;} public Boolean block2{get;set;} public String SalesPrice {get; set;} public integer Discount {get; set;} public String Quantity {get; set;} public String ServiceDate {get; set;} Id recordId; public ProductSearchPopupController(ApexPages.StandardController controller){ recordId = controller.getId(); system.debug('recordId '+recordId); wrapProductList = new List<wrapProduct>(); selectedWrapperList = new List<wrapProduct>(); normalList = true; selectedList = false; block = true; block1 = false; block2 = false; } public PageReference runQuery(){ if(query == null || query == ''){ system.debug('query '+query); ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info,'Please enter Product to search for')); return null; } system.debug('query '+query); List<List<PricebookEntry__c>> searchResults=[FIND :query IN ALL FIELDS RETURNING PricebookEntry__c (id, Name, ProductCode__c, Product2Id__r.Product_Description__c,UnitPrice__c, UseStandardPrice__c)]; if(searchResults[0]!=null){ for(PricebookEntry__c a: searchResults[0]) { // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList wrapProductList.add(new wrapProduct(a)); block = true; block1 = true; block2 = false; } } return null; } public PageReference ProceedWithSelectedToNextPage(){ selectedWrapperList = new List<wrapProduct>(); normalList = false; selectedList = true; for(wrapProduct selectedWrapObj: wrapProductList){ system.debug('selectedWrapObj.selected ---------'+selectedWrapObj.selected); if(selectedWrapObj.selected == true) selectedWrapperList.add(selectedWrapObj); } system.debug('selectedWrapperList size ---------'+selectedWrapperList.size()); PageReference pageRef = new PageReference('/apex/AccountOpportunityTwoPage'); pageRef.setRedirect(false); return pageRef; } public void processSelected() { selectedProduct = new List<PricebookEntry__c>(); for(wrapProduct wrapProductObj : wrapProductList) { if(wrapProductObj.selected == true) { selectedProduct.add(wrapProductObj.acc); block = false; block1 = false; block2 = true; } } } public class wrapProduct{ public PricebookEntry__c acc {get;set;} public Boolean selected {get;set;} public wrapProduct(PricebookEntry__c p) { this.acc = p; this.selected = false; } } public pagereference saveproduct(){ List<QuoteLineitem__c> quoteLineList = new List<QuoteLineitem__c>(); if(!selectedProduct.isEmpty()){ for(PricebookEntry__c sp:selectedProduct){ system.debug('sp '+sp); QuoteLineitem__c qli = new QuoteLineitem__c(); qli.QuotesId__c = recordId; qli.ListPrice__c = sp.UnitPrice__c; qli.UnitPrice__c = sp.UnitPrice__c; qli.Product2Id__c = sp.Product2Id__c; if(Discount!=0 || Discount!=null){ qli.Discount__c = Discount; } quoteLineList.add(qli); } if(quoteLineList.size()>0){ insert quoteLineList; PageReference pageRef = new PageReference('https://proseraa.lightning.force.com/lightning/r/Quotes__c/'+recordId+'/view'); pageRef.setRedirect(true); return pageRef; } } return null; } }
You can get the best practices to write a test class for a visualforce controller from below-
1. To get the minimum required 75% test coverage, all you have to do is make Salesforce run the code that you want to test.
2. Do not put (seeAllData = true) in test class otherwise, use it for exceptional cases.
3. Use @isTest at the Top for all the test classes.
4. Test in bulk: Test to see if your code can run on 200 records at once. This is the maximum number of records that can be evaluated in a single trigger by Salesforce. Don’t worry about this one too much for now, since we’ll be talking more about it in our Governor Limits chapter! It’s possible that your code runs fine if a single Contact goes through your trigger – but it might break if 200 Contacts are evaluated in your trigger!
5. Avoid Using Hard Coding Ids anywhere in test Class or any apex class.
6. Use System.runAs() method to test the functionality in user Context.you can use the following line of code for it-
Profile p = [select id from Profile where Name = ‘System Administrator’ LIMIT 1];
UserRole r = [Select id from UserRole where name = ‘CEO’];
User newUser = new User(FirstName=’Manpreet’, LastName=’Singh’, UserName=’singh2050′, Email=’singh2050@test.com’, Alias=’tester11′, LocaleSidKey=’en_US’, LanguageLocaleKey=’en_US’, EmailEncodingKey=’ISO-8859-1′, CommunityNickname=’singh2050.test’, ProfileId = p.Id, TimeZoneSidKey=’America/New_York’, isActive = true, UserRoleId = r.Id );
insert newUser;
System.runAs(newUser){
}
7. Make sure right tick must appear when you run your test class. Else check for errors.
8. Use System.assertEquals() to see if your code has the expected outcomes: for example, if your code changes a Contact’s education to ‘Graduate’, make sure it actually did by doing System.assertEquals(‘Graduate’, myContact.education__c);
9. Use Test.startTest() and Test.stopTest() statement to increase Governor Limits of your test code.
10. You have to ensure that every method in visualforce controller are covered through test class.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
You can use the setup method in your test class, this will improve best practices
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Regards,
Soyab
i am getting this error "System.ListException: List index out of bounds: 0" in the line 43
how do i correct this error??
Try this code.
Regards,
Soyab
i am still getting errors, actually i am working on the force.com platform, i have created all account, opportunity,quote as custom objects
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Account Name: id value of incorrect type: a033k00000QofwjAAB: [Account_Name__c], while inserting opportunity
I have taken account lookup field as (Account_Name__c), i still dont know where i am going wrong