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
SkyBlue2007SkyBlue2007 

Controller Extensions Test doesn't work !

Hi all,

 

I tried to test my Controller Extensions, but it didn't work.

 

Here is VF page whose name is "pageName". 

<apex:page standardController="User" extensions="userExtension2" sidebar="false">
<apex:pageMessages showDetail="true" />
 <apex:sectionHeader title="AAA" subtitle="{!$User.LastName} {!$User.FirstName}"></apex:sectionHeader>
    <apex:form >
        <apex:pageBlock title="BBB">
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="save"></apex:commandButton>
                <apex:commandButton action="{!cancel}" value="cancel"></apex:commandButton>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="CCC" collapsible="false" columns="1">
                <apex:inputField value="{!User.testField1__c}"></apex:inputField>
                <apex:inputField value="{!User.testField2__c}"></apex:inputField>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 Here is Controller Extensions.

public class userExtension2 {
        private final User u;
               
        public userExtension2(ApexPages.StandardController stdController) {                
           this.u = (User)stdController.getRecord();
           
        }
        
        public PageReference save() {
           if(u.testField1__c != null && u.testField1__c.length() > 0)
           {
              if(u.testField2__c != null && u.testField2__c.length() > 0)
              {
                 User us = [select testField1__c,testField2__c  from User where Id = :UserInfo.getUserId()];
                                 
                 us.testField1__c = u.testField1__c;
                 us.testField2__c = u.testField2__c;
                 
                 try {
                    //update
                    update us;
                    ApexPages.addMessage(new ApexPages.Message( ApexPages.severity.INFO, 'COMPLETE','COMPLETE'));
                 } catch (DmlException e) {
                    ApexPages.addMessage(new ApexPages.Message( ApexPages.severity.ERROR, 'ERROR','ERROR'));
                 }           
              }
              else
              {
                 ApexPages.addMessage(new ApexPages.Message( ApexPages.severity.ERROR, 'ERROR','ERROR'));
              }
           }
           else
           {
              ApexPages.addMessage(new ApexPages.Message( ApexPages.severity.ERROR, 'ERROR','ERROR'));
           }
           return null;     
        }
}

 

Here is Test Class.

public class userExtension2Tests {
   
   public static testMethod void testMyController() {
   
      PageReference pageRef = Page.pageName;
      Test.setCurrentPage(pageRef);
      
      User thisUser = [select testField1__c,testField2__c  from User where Id = :UserInfo.getUserId()];
      
      ApexPages.StandardController sc = new ApexPages.StandardController(thisUser);
      userExtension2 controller = new userExtension2(sc);
      
      String nextPage = controller.save().getUrl();
      System.assertEquals('/apex/pageName', nextPage);
      
      thisUser.testField1__c ='test12345@jp.fujitsu.com';
      thisUser.testField2__c = 'sample';
      nextPage = controller.save().getUrl();
      
      System.assertEquals('/apex/pageName', nextPage);
      
      User saveUser = [select testField1__c,testField2__c  from User where Id = :UserInfo.getUserId()];
      System.assertEquals('test12345@jp.fujitsu.com', saveUser.testField1__c);
      System.assertEquals('sample', saveUser.testField2__c);
   }
}

 

Here is Test Failures:

- Message is "System.NullPointerException: Attempt to de-reference a null object".

- Stack Trace is "Class.userExtension2Tests.testMyController: line 13, column 25 External entry point".

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

In this line of your test class:

 

 

 String nextPage = controller.save().getUrl();

 

 

You are invoking the getUrl method on the returned PageReference from your controller extension save method.

 

This method has the following return statement:

 

 

  return null;    

 

Which in Visualforce causes the page to be refreshed - it doesn't return a usable PageReference though. 

 

So you are trying to invoked a method on a null return value, which leads to the "attempt to dereference a null pointer".

 

All you can assert from the return value is that it is in fact null, which it will be in all cases.

All Answers

dotnet developedotnet develope

Hi,

 

You need to do couple of thinks in your code.

In main class

1) Define user object 'us' public at the top.

 

in test class

2) Run your code with runAs() method specifing the user.

 

 

bob_buzzardbob_buzzard

In this line of your test class:

 

 

 String nextPage = controller.save().getUrl();

 

 

You are invoking the getUrl method on the returned PageReference from your controller extension save method.

 

This method has the following return statement:

 

 

  return null;    

 

Which in Visualforce causes the page to be refreshed - it doesn't return a usable PageReference though. 

 

So you are trying to invoked a method on a null return value, which leads to the "attempt to dereference a null pointer".

 

All you can assert from the return value is that it is in fact null, which it will be in all cases.

This was selected as the best answer
SkyBlue2007SkyBlue2007

Hi bob_buzzard,

 

Thank you for your replay.

I've solved my problem.

I founded out that return value was "null" when I invoked the save method.

 

Thanks,

Seiji

SkyBlue2007SkyBlue2007

Hi,

 

Thank you for your reply.

It's helpful for me.

 

Thanks,

SkyBlue2007