0

Good Afternoon,

I am running my test class and i am getting the following error message.

Class.DM_MarketingCloudPolicyController.getPolicy: line 43, column 1 Class.DM_MarketingCloudPolicyController.getRedirect: line 52, column 1 Class.DM_MarketingCloudPolicyController_Test.DM_MarketingCloudPolicyController_Test: line 62, column 1"

Line 43 is Data_Policy__c dp = [SELECT Id, Name, Start_Date__c, MarketingCloudPolicyText__c FROM Data_Policy__c where Name like 'MarketingCloud%'];

Line 52 : Data_Policy__c dp = getPolicy();

I have looked at my controller and i dont understand why my test isnt passing. Looking forward to your help

This is my controller

    /********************************************************************
 * 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() { 
        // writes back Exact Target acceptance date to user on acceptance of policy
        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() { 
        // logs out user if user declines policy 
        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 where Name like 'MarketingCloud%'];

     // 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;
     }}

This is my unit test

    /********************************************************************
   *DM_MarketingCloudPolicyController_Test
   *
   *Test class for DM_MarketingCloudPolicyController
   *
   *Author: Daniel Mason 
   *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 
    ** Using Profile System admin X japan as  FIELD_CUSTOM_VALIDATION_EXCEPTION on System Admin profile
    ********************************************************************/

   @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 = '[email protected]';
    user1.Username = '[email protected]';
    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.Reason_for_Admin__c = 'CRM Team';
    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 = '[email protected]' LIMIT 1]);

    PageReference pageRef = Page.MarketingCloudPolicyPage;


    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 = '[email protected]'LIMIT 1];

    system.assertEquals(date.today(),user1.Marketing_Cloud_Acceptance_Date__c);
    Data_Policy__c dataPolicy = DM_MarketingCloudPolicyController.getPolicy();
    system.assertEquals(true,dataPolicy != null);
    test.stopTest();
    }
}
}
3
  • Welcome to SF.SE masond3. Without seeing the code from your test class, none of us can tell you what's wrong with your test class that you're not getting coverage of your code. Also, please take note that knowing the line number your problems occur isn't helpful since we can't tell the number when viewing it here, so mark you code sections to highlight them with asterisks or something.
    – crmprogdev
    Commented Sep 15, 2015 at 14:46
  • @Crmprogdev - I have now updated and included test class. I cant seem to get the formatting correct though
    – masond3
    Commented Sep 15, 2015 at 14:51
  • When you paste code into a post, select it, then click on the preformatted text icon {} to format it. Doing that, moves everything over by four spaces so it will appear properly and not cut-off active content. You can edit your post by clicking on the edit link at the bottom left.
    – crmprogdev
    Commented Sep 15, 2015 at 15:10

2 Answers 2

1

Below method would cause the issue in case when the query has not returned any record:-

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 where Name like 'MarketingCloud%'];

     // Data_Policy__c dp = [SELECT Id, Name, Start_Date__c, MarketingCloudPolicyText__c FROM Data_Policy__c LIMIT 1];             
        return dp;
}

First change it to:

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 where Name like 'MarketingCloud%'];

     // Data_Policy__c dp[] = [SELECT Id, Name, Start_Date__c, MarketingCloudPolicyText__c FROM Data_Policy__c LIMIT 1];             
        return dp.size()>0 ? dp[0] : null;
}


Also, re-verify the query and make sure that you get the record with the filter (where clause) you have used.

0

The source of your problem is your query parameters where Name like 'MarketingCloud%.

When you inserted the new Data_Policy__c, you specified: dp1.Name = 'Test Data Policy';, not something that looks like MarketingCloud.

Consequently, your query can't find the record you inserted. You don't really even need to query for the record. Once you've inserted it, it's simply dp1. It's Id will be dp1.Id. You can add a system.debug to verify it.

Many potential issues with test classes will become evident if you liberally insert debug statements, particularly before you activate system.asserts.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .