0

I am writing a unittest for my apex class but get an error System.NullPointerException: Attempt to de-reference a null object on my unittest for variable: testoptions

Apex class

public class WebToObjAttachments {
    public date TravelDateIn {get;set;}
    public date TravelDateOut {get;set;}

    public class ApplicationException extends Exception {}
    private final Lead weblead;
    public string recordtype{get;set;}
    public List<Attachment> attachments {get; set;} 
    public List<Attachment> newAttachments {get; set;}
    public string recordtypetext {get;set;}
    public blob file { get; set; }
    public string urlvalue=Apexpages.currentPage().getUrl();
        ..
            public List<SelectOption> CountryOptions {
            get {
                    List<SelectOption> options = new List<SelectOption>();
                if(!urlvalue.contains('_NL')){
                    options.add(new SelectOption('NL', 'Netherlands')); 
                    for (Schema.PicklistEntry pe : SObjectType.Lead.fields.CountryCode.getPicklistValues()) {

                    }
                    options.add(new SelectOption('BE', 'Belgium')); 
                    options.add(new SelectOption('FR', 'France')); 
                    options.add(new SelectOption('DE', 'Germany')); 
                    options.add(new SelectOption('ES', 'Spain')); 
                    options.add(new SelectOption('PT', 'Portugal')); 
                    options.add(new SelectOption('IT', 'Italy')); 
                    options.add(new SelectOption('GB', 'United Kingdom')); 
                    options.add(new SelectOption('SCOT', 'Scotland')); 
                    options.add(new SelectOption('IE', 'Ireland')); 
                    options.add(new SelectOption('US', 'United States')); 
                }
             return options;
    } 
        set;}
        ..
        }

Unittest

 private static testMethod void CountryOptionsTest()
{
    property__c prop = new property__c();
    prop.name = 'Testproperty';
    insert prop;

    Lead le=new Lead(Lastname='Unit Test', RecordType__c = 'Guest');
    insert le;
    ApexPages.StandardController sc = new ApexPages.standardController(le);
    WebToObjAttachments controller = new WebToObjAttachments(sc);
    PageReference pageRef = Page.WebToObjAttachment_NL;

    Test.startTest();
    Test.setCurrentPage(pageRef);
    List<SelectOption> testoptions = controller.CountryOptions ; 
    Test.stopTest();
}

Bebug on urlvalue returms null what am I doing wrong.

Blockquote

  • 1
    I assume that urlvalue is null since there are no Test.setPage() in the unit test, therefore it is not being set and it cause null pointer exception – kurunve Mar 29 at 20:46
2

What you've done in your test class is instatiate your apex controller, but you haven't provided any context for that controller. In other words, your getURL() in the controller is based on the current page and you need to set this in your unit test. This answer goes over why you need to use it as you might have a single controller for different pages. You want to set the context and test for the behavior you're expecting from the controller for a specific page.

You can do this by creating a PageReference with your Visualforce page

PageReference pageRef = Page.VisualforcePageName;

and then setting that as the current page in test using setCurrentPage().

Test.setCurrentPage(pageRef);

After you've done the two things above, you can do what you were doing (ApexPages.standardController and instantiating your controller)

| improve this answer | |
  • I updated my test as you suggested but stil get the same error. – Thomas Mar 29 at 21:18
  • You need to make the PageReference and Test.setCurrentPage lines occur first, before the controller line as the controller relies on the current page being set already. – Kris Goncalves Mar 29 at 22:16

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.