+ Start a Discussion
rupesh ranjanrupesh ranjan 

Any one suggest for Test class for below constructor of main class

public class OSM_SW_TaxExemptionContactCtrl {
    private account primaryaccount ;    
    
    public  OSM_SW_TaxExemptionContactCtrl(){
        Id recid = apexpages.currentpage().getparameters().get('accountid');
        if(recid!=null){
               primaryaccount  = [select id,Customer_BMS_number__c,BOLT_Account_Number__c,IAM_Service_Provider_Code__c,BMS_Customer_ID__c,BMS_Customer_ID_Junction__c from account where id=:recid limit 1];
           if(primaryaccount.Customer_BMS_number__c!=null){
              CustomerNumber= primaryaccount.Customer_BMS_number__c; 
            }else if(primaryaccount.BOLT_Account_Number__c!=null){
                CustomerNumber= primaryaccount.BOLT_Account_Number__c; 
            }else {
                 CustomerNumber= primaryaccount.IAM_Service_Provider_Code__c;
            }
        }
       String myGeneratedFileName = 'Tax Exemption ContactForm.pdf';
       Apexpages.currentPage().getHeaders().put('content-disposition', 'attachment; filename='+myGeneratedFilename);
       Apexpages.currentPage().getHeaders().put('content-disposition', 'inline; filename='+myGeneratedFilename); 
    }
]

 
Agustin BAgustin B
Hi Rupesh, please check this link that will help you with an example of what you want to accomplish

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm
You have to create an account, set the page with the parameter (id of the account created) and after that you can validate the headers to see if everything worked out.

If it solved your issue please mark it as the correct answer, it may help others.
rupesh ranjanrupesh ranjan
Hi Agusin Still my test class is covering 71 %
 
@isTest
    global class OSM_SW_TaxExemptionContactCtrl_Test{
       public static testmethod void testTaxExemptionContactCtrl() {
        //Create account
      
           
           Account testaccount = new Account();
                testAccount.Name='Test Account' ;
                testAccount.BOLT_Account_Number__c='1345' ;
                testAccount.IAM_Service_Provider_Code__c='Test Account' ;
                testAccount.BMS_Customer_ID__c='3456' ;
                testAccount.BMS_Customer_ID_Junction__c ='34567' ;      
                insert testAccount;
    
          
           test.startTest();
           
                 OSM_SW_TaxExemptionContactCtrl ctrl = new OSM_SW_TaxExemptionContactCtrl();
          //System.assertNotEquals(null,testAccount.id);
            //ctrl.Accountid = testAccount.id;
           ctrl.downloadcontactform();
           
           List<SelectOption> options = ctrl.getstates();
            //PageReference pageRef = Page.TestattachPDF;
            //Test.setCurrentPage(pageRef);
            ctrl.renderingAsHtml();
            ctrl.saveToPdf();
            test.stopTest();
}
}

below code is not  covering
if(recid!=null){
               primaryaccount  = [select id,Customer_BMS_number__c,BOLT_Account_Number__c,IAM_Service_Provider_Code__c,BMS_Customer_ID__c,BMS_Customer_ID_Junction__c from account where id=:recid limit 1];
           if(primaryaccount.Customer_BMS_number__c!=null){
              CustomerNumber= primaryaccount.Customer_BMS_number__c; 
            }else if(primaryaccount.BOLT_Account_Number__c!=null){
                CustomerNumber= primaryaccount.BOLT_Account_Number__c; 
            }else {
                 CustomerNumber= primaryaccount.IAM_Service_Provider_Code__c;
            }

 
Agustin BAgustin B
Hi Rupesh, please read the link again, you should assign the parameter to the page so you can retrieve that id when you are testing the controller.
Something like:
PageReference pageRef = --YOUR PAGE--;
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('accountid', '--YOURID--');

Please let me know if this help you solve your issue