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
CRM ADMIN 19CRM ADMIN 19 

Constructor not defined - TestClass - Controller extension

Hi,

I would be really grateful if someone can help me! I'm getting crazy trying to write a test class for an extended class used in a visualforce. Now I'm getting next error but I can't really understand

Error: Compile Error: Constructor not defined: [IN_Local_SalesEdit].<Constructor>(ApexPages.StandardController) at line 17 column 40

Here is my class

public with sharing class IN_Local_SalesEdit {
    ApexPages.StandardSetController con ;
    
    List<IN_Local_Sales__c> editLS = new List<IN_Local_Sales__c>();
    
    string strAccountAuxId;
    date currentDate;
      
    public IN_Local_SalesEdit(ApexPages.StandardSetController controller) {
          this.con = controller ; 
          string strAccountId; 
          strAccountId = ApexPages.currentPage().getParameters().get('id');
          strAccountAuxId = strAccountId ;
          editLS = con.getSelected();
          
          currentDate = Date.today();
          
          Set <Id> aIds = new Set<Id>();
          for(IN_Local_Sales__c act : editLS){
            aIds.add(act.Id);
          }
          editLS = [SELECT In_market_Sales_UN_Real__c,In_market_Sales_VAL_Real__c, Month__c, Name, Id,Purchases__c,Stock_at_the_end_of_month__c,Price_ExFactory__c, Producto__c,Comments__c, Country__c FROM IN_Local_Sales__c WHERE Account__c=:strAccountId AND MonthNumber__c =: currentDate.month()-1 ORDER BY Month__c,Country__c,Producto__c ASC];       
          con.setSelected(editLS); 
    }
    
    public PageReference Filter() {
          
          String strMonth = Apexpages.currentPage().getParameters().get('months'); 
          String strCountry = Apexpages.currentPage().getParameters().get('countries');
          
                if(!strMonth.equals(''))
                {
                    if (!strCountry.equals(''))
                    {
                        editLS = [SELECT In_market_Sales_UN_Real__c,In_market_Sales_VAL_Real__c, Month__c, Name, Id,Purchases__c,Stock_at_the_end_of_month__c,Price_ExFactory__c, Producto__c,Comments__c, Country__c FROM IN_Local_Sales__c WHERE Account__c=:strAccountAuxId AND Month__c =:strMonth AND Country__c =:strCountry AND MonthNumber__c <:currentDate.month() ORDER BY Month__c,Country__c,Producto__c ASC];
                    }
                    else
                    {
                        editLS = [SELECT In_market_Sales_UN_Real__c,In_market_Sales_VAL_Real__c, Month__c, Name, Id,Purchases__c,Stock_at_the_end_of_month__c,Price_ExFactory__c, Producto__c,Comments__c, Country__c FROM IN_Local_Sales__c WHERE Account__c=:strAccountAuxId AND Month__c =:strMonth AND MonthNumber__c <:currentDate.month() ORDER BY Month__c,Country__c,Producto__c ASC];
                    }
                }
                else
                {   
                    if (!strCountry.equals(''))
                    {
                        editLS = [SELECT In_market_Sales_UN_Real__c,In_market_Sales_VAL_Real__c, Month__c, Name, Id,Purchases__c,Stock_at_the_end_of_month__c,Price_ExFactory__c, Producto__c,Comments__c, Country__c FROM IN_Local_Sales__c WHERE Account__c=:strAccountAuxId AND Country__c =:strCountry AND MonthNumber__c <:currentDate.month() ORDER BY Month__c,Country__c,Producto__c ASC];
                    }
                    else
                    {
                        editLS = [SELECT In_market_Sales_UN_Real__c,In_market_Sales_VAL_Real__c, Month__c, Name, Id,Purchases__c,Stock_at_the_end_of_month__c,Price_ExFactory__c, Producto__c,Comments__c, Country__c FROM IN_Local_Sales__c WHERE Account__c=:strAccountAuxId AND MonthNumber__c <:currentDate.month() ORDER BY Month__c,Country__c,Producto__c ASC];
                    }
                }   
                con.setSelected(editLS);
                return null;
            }  
    
    public List<String> months{
    get {
      if (months == null) {
 
        months = new List<String>();
        Schema.DescribeFieldResult field = IN_Local_Sales__c.Month__c.getDescribe();
 
        for (Schema.PicklistEntry f : field.getPicklistValues())
          months.add(f.getLabel());
 
      }
      return months;          
    }
    set;
  }
  public List<String> countries{
    get {
      if (countries== null) {
 
        countries= new List<String>();
        Schema.DescribeFieldResult field = IN_Local_Sales__c.Country__c.getDescribe();
        for (Schema.PicklistEntry f : field.getPicklistValues())
          countries.add(f.getLabel());
 
      }
      return countries;          
    }
    set;
  }
  public PageReference customSave(){

    PageReference pr = ApexPages.currentPage();
    
    try {
        upsert editLS ;
        pr.setRedirect(true);
    }
    catch (Exception e) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops Complete mandatory info please!'));
    }
    
    return pr;
  }
    
}

My Test Class

@isTest
public class testIN_Local_Sales_Edit {
    static testMethod void IN_Local_Sales_Edit_Test()
    {
        //Test converage for the myPage visualforce page
        PageReference pageRef = Page.IN_Local_Sales_Edit;
        Test.setCurrentPageReference(pageRef);
        Account newAccount = new Account (name='XYZ Organization');
        insert newAccount;
        //create first sales record
        IN_Local_Sales__c mySales = new IN_Local_Sales__c (In_market_Sales_UN_Bdg__c=1000,
        Account__c=newAccount.id);
        insert mySales;
        ApexPages.StandardController sc = new ApexPages.standardController(mySales);
        // create an instance of the controller
        IN_Local_SalesEdit myPageCon = new IN_Local_SalesEdit (sc);
      
    }
}

And myPage

<apex:page StandardController="IN_Local_Sales__c" recordSetVar="IN_Local_Sales__c" extensions="IN_Local_SalesEdit"> <apex:form > <apex:pageBlock title="Filters" mode="edit" id="criteria"> <script type="text/javascript"> function doSearch() { searchServer( document.getElementById("months").value, document.getElementById("countries").value) ; } </script> <apex:actionFunction name="searchServer" action="{!Filter}" rerender="results"> <apex:param name="months" value="" /> <apex:param name="countries" value="" /> </apex:actionFunction> <table> <tr> <td style="font-weight:bold;">Month<br/> <select id="months" onchange="doSearch();"> <option value=""></option> <apex:repeat value="{!months}" var="mon"> <option value="{!mon}">{!mon}</option> </apex:repeat> </select> </td> </tr> <tr> <td style="font-weight:bold;">Country<br/> <select id="countries" onchange="doSearch();"> <option value=""></option> <apex:repeat value="{!countries}" var="con"> <option value="{!con}">{!con}</option> </apex:repeat> </select> </td> </tr> </table> </apex:pageBlock> <apex:pageBlock title="Edit Local Sales" mode="edit" id="results"> <apex:pageBlockButtons location="top"> <apex:commandButton value="Save" action="{!customsave}"/> <apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons> <apex:pageMessages /> <apex:pageBlockTable value="{!selected}" var="ql"> <apex:column headerValue="Month"><apex:outputField value="{!ql.Month__c}"/></apex:column> <apex:column headerValue="Country"><apex:outputField value="{!ql.Country__c}"/></apex:column> <apex:column headerValue="Product SKU"><apex:outputField value="{!ql.Producto__c}"/></apex:column> <apex:column headerValue="Market Sales UN"><apex:inputField value="{!ql.In_market_Sales_UN_Real__c}" /></apex:column> <apex:column headerValue="Market Sales VAL"><apex:inputField value="{!ql.In_market_Sales_VAL_Real__c}" /></apex:column> <apex:column headerValue="Purchases"><apex:inputField value="{!ql.Purchases__c}" /></apex:column> <apex:column headerValue="Stock at the end of month"><apex:inputField value="{!ql.Stock_at_the_end_of_month__c}" /></apex:column> <apex:column headerValue="Price ExFactory"><apex:inputField value="{!ql.Price_ExFactory__c}" /></apex:column> <apex:column headerValue="Comments"><apex:inputField value="{!ql.Comments__c}"/></apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>

Could you help me please?

Thanks in advance!
Ivan
Best Answer chosen by CRM ADMIN 19
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for test classes.
1) http://amitsalesforce.blogspot.in/search/label/Test%20Class
2) http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

For StandardSetController  your constructor should be like below:-

Try below test class.
 
@isTest
public class testIN_Local_Sales_Edit {
    static testMethod void IN_Local_Sales_Edit_Test()
    {
	
        PageReference pageRef = Page.IN_Local_Sales_Edit;
        Test.setCurrentPageReference(pageRef);
		
        Account newAccount = new Account (name='XYZ Organization');
        insert newAccount;
		
        IN_Local_Sales__c mySales = new IN_Local_Sales__c (In_market_Sales_UN_Bdg__c=1000, Account__c=newAccount.id);
        insert mySales;
        
        IN_Local_Sales__c [] newSalesList= [select id,name from IN_Local_Sales__c where Account__c =: newAccount.id];


		PageReference pageRef = Page.AccountPlan; // Add your page Name here
		pageRef.getParameters().put('id', String.valueOf(newAccount.Id));
  
        ApexPages.StandardSetController sc = new ApexPages.standardSetController(newSalesList);
        sc.setSelected(newSalesList);
		
        IN_Local_SalesEdit myPageCon = new IN_Local_SalesEdit(sc);
		
        
        apexpages.currentpage().getparameters().put('months' , 'FEB');
        apexpages.currentpage().getparameters().put('countries' , 'Austria');
	
        myPageCon.Filter();
        myPageCon.customSave();
		
		List<String>  lstMonths = myPageCon.months;
		List<String>  lstCountries = myPageCon.countries;
        
		
    }
}



Please follow below salesforce Best Practice for Test Classes :-

1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method  is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't  send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method  and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .


Please let us know if this post will help you
 

All Answers

CRM ADMIN 19CRM ADMIN 19
Hi,

I'm sorry I understood why!! It should be a set of records.

However the coverage is not really high... how can I improve for an extended controller ??

Thanks
Ivan
CRM ADMIN 19CRM ADMIN 19
Hi,
I forgot to add the lines that I have in this purpose, so my test class is this one, but I've only 76%.
Thanks
Ivan

@isTest
public class testIN_Local_Sales_Edit {
    static testMethod void IN_Local_Sales_Edit_Test()
    {
        //Test converage for the myPage visualforce page
        PageReference pageRef = Page.IN_Local_Sales_Edit;
        Test.setCurrentPageReference(pageRef);
        Account newAccount = new Account (name='XYZ Organization');
        insert newAccount;
        //create first sales record
        IN_Local_Sales__c mySales = new IN_Local_Sales__c (In_market_Sales_UN_Bdg__c=1000,
        Account__c=newAccount.id);
        insert mySales;
        
        IN_Local_Sales__c [] newSalesList= [select id,name from IN_Local_Sales__c where Account__c =: newAccount.id];
        
        ApexPages.StandardSetController sc = new ApexPages.standardSetController(newSalesList);
        // create an instance of the controller
        
        IN_Local_SalesEdit myPageCon = new IN_Local_SalesEdit (sc);
        
        //try calling methods/properties of the controller in all possible scenarios
        // to get the best coverage.
        
        apexpages.currentpage().getparameters().put('months' , 'FEB');
        apexpages.currentpage().getparameters().put('countries' , 'Austria');
        
        myPageCon.Filter();
        myPageCon.customSave();
        
    }
}
 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for test classes.
1) http://amitsalesforce.blogspot.in/search/label/Test%20Class
2) http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

For StandardSetController  your constructor should be like below:-

Try below test class.
 
@isTest
public class testIN_Local_Sales_Edit {
    static testMethod void IN_Local_Sales_Edit_Test()
    {
	
        PageReference pageRef = Page.IN_Local_Sales_Edit;
        Test.setCurrentPageReference(pageRef);
		
        Account newAccount = new Account (name='XYZ Organization');
        insert newAccount;
		
        IN_Local_Sales__c mySales = new IN_Local_Sales__c (In_market_Sales_UN_Bdg__c=1000, Account__c=newAccount.id);
        insert mySales;
        
        IN_Local_Sales__c [] newSalesList= [select id,name from IN_Local_Sales__c where Account__c =: newAccount.id];


		PageReference pageRef = Page.AccountPlan; // Add your page Name here
		pageRef.getParameters().put('id', String.valueOf(newAccount.Id));
  
        ApexPages.StandardSetController sc = new ApexPages.standardSetController(newSalesList);
        sc.setSelected(newSalesList);
		
        IN_Local_SalesEdit myPageCon = new IN_Local_SalesEdit(sc);
		
        
        apexpages.currentpage().getparameters().put('months' , 'FEB');
        apexpages.currentpage().getparameters().put('countries' , 'Austria');
	
        myPageCon.Filter();
        myPageCon.customSave();
		
		List<String>  lstMonths = myPageCon.months;
		List<String>  lstCountries = myPageCon.countries;
        
		
    }
}



Please follow below salesforce Best Practice for Test Classes :-

1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method  is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't  send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method  and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .


Please let us know if this post will help you
 
This was selected as the best answer
CRM ADMIN 19CRM ADMIN 19
Thanks a lot for your comments Amit
Regards
Ivan