0

I have a PageReference method as given below I am trying to write a test class for this it shows error.

Error Message:

System.NullPointerException: Argument cannot be null.

Stack Trace:

Class.System.EncodingUtil.base64Decode: line 5, column 1 Class.TestUnsusbcribe.Unsubscribe: line 12, column 1

how to pass the 'action' value in this Blob method (Blob decodedBlob = EncodingUtil.base64Decode(action);) (line 12)

please any one give some idea to solve this problem

public without sharing class TestUnsusbcribe {  
    public String compid {get;set;}
    public String action {get;set;}     
    public TestUnsusbcribe(){
         compid = ApexPages.CurrentPage().getParameters().get('Id');
         action = ApexPages.CurrentPage().getParameters().get('type');         

    }   
    public PageReference Unsubscribe(){    
        Blob decodedBlob = EncodingUtil.base64Decode(action);
        String actions = decodedBlob.tostring();       
        if(actions =='emp'){
            Employee__c ageobj = [SELECT Id,Name,Communication_Agreement__c,Email__c FROM Employee__c WHERE Id=:compid];
            if(ageobj !=null){
                ageobj.Communication_Agreement__c = false;
                update ageobj;                
                list<org__c> compobj = [SELECT Id,Name,Communication_Agreement__c,email__c FROM org__c WHERE email__c=:ageobj.Email__c limit 1];

                if(compobj.size()>0 && !compobj.isempty() )
                {
                    compobj[0].Communication_Agreement__c = false;
                    update compobj;
                }                
                pagereference p = apexpages.Currentpage();
                apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'You Successfully opt-out the email communication');
                apexpages.addmessage(msg);
                return p; 
            }
        }
        return null;
    }

}

Test Class:

           @isTest
           Public class Test_TestUnsusbcribe {

           static testMethod void UnsubscribeTest() {

            Test.startTest();

                Employee__c  emply = new Employee__c (First_Name__c='test987',Last_Name__c='test654',Email__c='test9@gmail.com');
                insert emply;

                org__c cmpny= new org__c(Name='test3 User3',email__c='test9@gmail.com',password__c='1234567',Communication_Agreement__c=true);
                Insert cmpny;


                TestUnsusbcribe  Unsusb=new TestUnsusbcribe ();
                String action = EncodingUtil.base64Encode(Blob.valueOf('emp'));
                     Cookie cookie = new Cookie('test9@gmail.com', action, null, 31536000, false);
                    ApexPages.currentPage().setCookies(new Cookie[]{cookie});
                Unsusb.Unsubscribe();

                Test.stopTest();

           }

        }
  • Post your test class – Ankita Jul 22 '17 at 10:50
  • @Ankita now added test class also please check – Sfdcprgmr Jul 22 '17 at 11:01
2

You need to pass the action and compId through Page Reference in the test class.

Try this:

@isTest
Public class Test_TestUnsusbcribe {

    static testMethod void UnsubscribeTest() {
        Test.startTest();

        Employee__c  emply = new Employee__c (First_Name__c='test987',Last_Name__c='test654',Email__c='test9@gmail.com');
        insert emply;

        org__c cmpny= new org__c(Name='test3 User3',email__c='test9@gmail.com',password__c='1234567',Communication_Agreement__c=true);
        Insert cmpny;

        String action = EncodingUtil.base64Encode(Blob.valueOf('emp'));
        Cookie cookie = new Cookie('test9@gmail.com', action, null, 31536000, false);
        // Pass the page name
        PageReference pageRef = Page.PageName;
        Test.setCurrentPage(pageRef);
        pageRef.getParameters().put('type', action);
        pageRef.getParameters().put('Id', cmpny.Id);            
        pageRef.setCookies(new Cookie[]{cookie});

        TestUnsusbcribe  Unsusb=new TestUnsusbcribe ();
        Unsusb.Unsubscribe();

        Test.stopTest();

       }

    }
| improve this answer | |
  • Thank you so much @Ankita, you are entirely correct. I've updated my testclass for your reference now it's working fine. – Sfdcprgmr Jul 25 '17 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.