3

I have attempted to write my first test apex class, and although it looks correct when i try and save i get the following error

"Method does not exist or incorrect signature: getPolicy() at line 74 column 37".

I have been looking at this class for the last 2 hours trying to work out what the problem is, however i am keep drawing a blank.

/********************************************************************
 *DM_MarketingCloudPolicyController_Test
 *
 *Test class for DM_MarketingCloudPolicyController
 *
 *Author:
 *Created: 14/09/2015
 *The @isTest class annotation indicates this class only contains test methods.
 *Classes defined with the @isTest annotation do not count against the organization 
 *size limit for all Apex scripts.See the Apex Language Reference for more information
 *about Testing and Code Coverage. Look @ BG coding standards if any doubts 
 ********************************************************************/

@isTest 
private class  DM_MarketingCloudPolicyController_Test{
static testMethod void DM_MarketingCloudPolicyController_Test(){

/*
*  Test Accept & Decline of Marketing Cloude policy
*/

        Profile prof1 = [SELECT Id, Name FROM Profile  WHERE Name = 'System Administrator'];


        User user1 = new User();
        user1.FirstName = 'Daniel';
        user1.LastName = 'Mason';
        user1.Email = 'daniel.mason@schroders.com';
        user1.Username = 'daniel.mason@schroders.com';
        user1.Alias = 'DMaso';
        user1.CommunityNickname = 'DMaso';
        user1.TimeZoneSidKey = 'America/Los_Angeles';
        user1.LocaleSidKey = 'en_US';
        user1.EmailEncodingKey = 'UTF-8';
        user1.ProfileId = prof1.Id;
        user1.LanguageLocaleKey = 'en_US';
        user1.Marketing_Cloud_Acceptance_Date__c = null;
        insert user1;

//Setup Custom Setting for profile
        Data_Policy_Settings__c dps = new Data_Policy_Settings__c();
        dps.Show_Marketing_Policy__c = true;
        dps.SetupOwnerId = prof1.Id;

//Create Test Policy
        Data_Policy__c dp1 = new Data_Policy__c();
        dp1.Name = 'Test Data Policy';
        dp1.Start_Date__c = date.today();
        dp1.MarketingCloudPolicyText__c = 'Everyone Loves creating test classes ...';
        insert dp1;

        system.assertEquals(1, [SELECT Count() FROM User WHERE email = 'daniel.mason@schroders.com' LIMIT 1]);

        PageReference pageRef = Page.DataPolicyPage;


        System.runAs(user1){
        test.startTest();
        DM_MarketingCloudPolicyController controllerInstance = new DM_MarketingCloudPolicyController();
        boolean redirect = controllerInstance.getRedirect();

        test.setCurrentPage(pageRef);
        DM_MarketingCloudPolicyController controller = new DM_MarketingCloudPolicyController();

//Declining VF Page  
        PageReference logout = controller.closePopupDecline();
        system.assertEquals('/secur/logout.jsp',logout.getURL());

//Accepting VF Page
        controller.closePopupAccept();
        user1 = [SELECT Marketing_Cloud_Acceptance_Date__c FROM User WHERE email = 'daniel.mason@schroders.com'LIMIT 1];

        system.assertEquals(date.today(),user1.Marketing_Cloud_Acceptance_Date__c);
        Data_Policy__c dataPolicy = getPolicy();
        system.assertEquals(true,dataPolicy != null);
        test.stopTest();
        }
    }
}

Here is my controller class

   /********************************************************************
 * DM_MarketingCloudPolicyController
 *
 * Controller class for MarketingCloudPolicyPage
 * 
 * Author: Daniel Mason
 * Created: 14/09/2015
 * add et4ae5__ExactTargetForAppExchangeUser__c = True logic userloggedin
 * 
 ********************************************************************/

public class DM_MarketingCloudPolicyController{

    Id uid; 
    User loggedInUser;

    public DM_MarketingCloudPolicyController(){
        uid = UserInfo.getUserId();
        loggedInUser = [SELECT Id, Marketing_Cloud_Acceptance_Date__c 
              FROM User
              WHERE Id = :uid
              LIMIT 1];
    } 

    public PageReference closePopupAccept() { 
        // Method to write back data policy acceptance date to user on accept
        loggedInUser.Marketing_Cloud_Acceptance_Date__c = date.today();
        update loggedInUser;        
        PageReference p = new PageReference('/home/home.jsp');
        p.setRedirect(true);      
        return p;
    } 

    public PageReference closePopupDecline() { 
        // Method to logout user afer decline 
        PageReference p = new PageReference('/secur/logout.jsp');
        p.setRedirect(true);      
        return p; 
    }

    public static Data_Policy__c getPolicy(){
        // Method to obtain data policy text
        Data_Policy__c dp = [SELECT Id, Name, Start_Date__c, MarketingCloudPolicyText__c 
                             FROM Data_Policy__c
                             LIMIT 1];

        return dp;
    }

   public boolean getRedirect(){
        // Method to determine show popup
        boolean redirect = false;     
        Data_Policy__c dp = getPolicy(); 
        Data_Policy_Settings__c dps = Data_Policy_Settings__c.getInstance(UserInfo.getProfileId());            
        if(dps.Show_Marketing_Policy__c == true && (loggedInUser.Marketing_Cloud_Acceptance_Date__c == null && loggedInUser.et4ae5__ExactTargetForAppExchangeUser__c == True || loggedInUser.Marketing_Cloud_Acceptance_Date__c.addYears(1) < date.today() || loggedInUser.Marketing_Cloud_Acceptance_Date__c < dp.Start_Date__c)){                              
            redirect = true;
        } 
        return redirect;
     }}
1
  • 1
    As the error clearly saying, I can't see a getPolicy() method in this class. Did you check for that? – highfive Sep 14 '15 at 11:38
0

I believe it's in this line

Data_Policy__c dataPolicy = getPolicy();

seeing as this is a test class, I am guessing this is a method in the DM_MarketingCloudPolicyController class. Since it is a controller method, you need to call the method on the instance of the controller you have already created. I believe if you adjust the line to this, it should work

Data_Policy__c dataPolicy = controller.getPolicy();

EDIT

If it is a static method, then try calling it directly on the class rather than an instance of the class

Data_Policy__c dataPolicy = DM_MarketingCloudPolicyController.getPolicy();
8
  • if i change that line i get the following error Compile Error: Static methods cannot be invoked through an object instance: getPolicy() at line 74 column 37 – Masond Sep 14 '15 at 11:46
  • if i change getPolicy(); to controller.getPolicy(); i get the following error Compile Error: Static methods cannot be invoked through an object instance: getPolicy() at line 74 column 37 – Masond Sep 14 '15 at 11:51
  • oh, if its a static method then you call it on the class itself, and not an instance of the class. See the edit to my answer – Chris Duncombe Sep 14 '15 at 11:53
  • No problem, glad it worked :) – Chris Duncombe Sep 14 '15 at 12:00
  • Well the test class passed and my code has 95% coverage, however i was expecting a certain reasult and this didnt happen. Hmm – Masond Sep 14 '15 at 12:02

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.