Skip to main content

Unit Test Fail - list has no rows for assignment to Sobject

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();
    }
}
}