+ Start a Discussion
Ben.SchauerhamerBen.Schauerhamer 

Apex Test Class is ignoring Custom Setting in a Validation Rule

I have created a simple Validation rule that states if Record Name = "RecordX" and Custom Setting Can Edit All Records != TRUE then you cannot edit the record. 

I have one test that is not working with the validation rule. I have put in the debug logs the current user and it is running with my user which it should. I can edit the record through the UI but the test validation rule is in place with the test.

Test Method:
@istest
public with sharing class OomasyncpageeExtension_test{
    testmethod static void OSPETmethod(){
        //create data
        
        User U = [SELECT Id FROM User WHERE Profile.Name='System Administrator' and isActive=true LIMIT 1];
        system.debug('User U::--::'+U);
        
        Ooma__c OCS_one = new Ooma__c (Name = 'Change Info', End_Point_URL__c = 'http://site.com' );
        insert OCS_one;
        
        ooma__c OCS_two = new Ooma__c (Name = 'Change or Cancel', End_Point_URL__c = 'site.com');
        insert OCS_two;
        
        id OSRT = Schema.SObjectType.service__c.getRecordTypeInfosByName().get('Voice Service').getRecordTypeId();
        id OOrt = Schema.SObjectType.opportunity.getRecordTypeInfosByName().get('Voice: New Sale').getRecordTypeId();
        
        Account acct = new Account(Name = 'First last');
        insert acct;
        
        Contact con = new Contact(AccountId = acct.Id, FirstName = 'first', LastName = 'last', EMail = 'someone@somewhere.com');
        insert con;
        
        Zuora__CustomerAccount__c bAcc=new Zuora__CustomerAccount__c();
        bAcc.Name='Test Bill';
        bAcc.Zuora__Account__c=acct.Id; 
        bAcc.Zuora__BillCycleDay__c = '1st of the month';
        bAcc.Zuora__BillToAddress1__c = '125 fake street';
        bAcc.Zuora__BillToAddress2__c = '';
        bAcc.Zuora__BillToState__c = 'UT';
        bAcc.Zuora__BillToPostalCode__c = '84058';
        bAcc.Zuora__BillToCity__c = 'Provo';
        bAcc.Zuora__BillToCountry__c = 'US';
        bAcc.Send_to_Collections__c = false;
        bAcc.Cancel_Billing_Account__c = false;
        Insert bAcc;
        
         Vivint_Employee__C VE = new Vivint_Employee__C (Name = 'Testname', Employee_ID__c ='999990');
        insert VE;
        system.debug('Inserted VE');
        
        opportunity oppty = new opportunity();
        Oppty.AccountId = acct.id;
        oppty.Billing_Address_Same_As_Service__c = true;
        oppty.Sales_Credit__c = VE.id;
        oppty.CloseDate = date.today();
        oppty.stageName = 'Submit For Enrollment';
        oppty.Billing_Account__c = bAcc.id;
        oppty.Contract_Signer__c = con.id;
        oppty.Service_Address_1__c = '125 fake street';
        oppty.Service_Address_2__c = '';
        oppty.Service_Address_City__c = 'Fake City';
        oppty.Service_Address_Postal_Code__c = '55555';
        oppty.Service_Address_State__c = 'CA';
        oppty.Service_Address_Country__c = 'US';
        oppty.Service_Level__c = 'Standard';
        oppty.RecordTypeId = oort;
        oppty.Name = 'Test Oppty';
        insert oppty;
        
        service__c OS = new Service__c (Account__c = acct.id, Initial_Opportunity__c = oppty.Id , Billing_Account__c = bAcc.id, Status__c = 'Active',  VOIP_Hardware_Serial_Number__c = 'Hardware serial 1', VOIP_Hardware_Serial_Number2__c= 'Hardware serial 2', RecordTypeId = OSRT, VOIP_internal_ID__c = '1234', VOIP_E911_State__c = 'CA', VOIP_E911_Postal_Code__c = '55555', VOIP_E911_Country_Code__c = 'US' ,VOIP_E911_City__c = 'test city', VOIP_E911_Address__c = 'test address', Service_level__c = 'Basic');
        OS.Contract_Signer__c = con.id;
        System.debug('Debug::: Service Record: ' + OS);
        System.debug('Current User: ' + UserInfo.getUserName());
        System.debug('Current Profile: ' + UserInfo.getProfileId());
        insert OS; 
        
        PageReference PG = Page.oomaSyncPage;
        Test.setCurrentPage(PG);
        ApexPages.currentPage().getParameters().put('id', string.valueOf(OS.id));
        Apexpages.standardcontroller stdcontroller = new Apexpages.standardcontroller(OS);
        OOmaSyncPageExtension a = new OOmaSyncPageExtension(stdcontroller);
        
        Test.startTest(); 
        Test.setMock(HttpCalloutMock.class, new OOmasyncpageextMHRG());
        
        a.oomacontactsync();
        a.nosync();
        Test.Stoptest();
    }//closing testmethod
}// closing class

Validation Rule:
AND( RecordType.DeveloperName = 'Voice_Record_Type',  !$Setup.Can_Modify_All_Services__c.Bypass_Service_Validation_Rules__c )

As mentioned before the Debug log says the Current User is me and the Profile Id is the Sys Admin Id. 
 
Best Answer chosen by Ben.Schauerhamer
Sforce.NinjaSforce.Ninja
Yo Ben,
Did not understand a lot of what you meant in the post but what I understand is, your test class is ignoring the custom setting. It is because it cannot see the setting. 

Custom Settings are "Data" and as such are not part of the test-environment as of Spring '12. 

There are two ways you can fix this issue

1 insert new CustomSetting__c(Field__c = 'Value');

2. Or you could add the following annotation to your testmethod: (see http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm)
 
@isTest(SeeAllData=true)
public static testMethod void anAwesomeTestMethod() {

     ...

 }

 

All Answers

Sforce.NinjaSforce.Ninja
Yo Ben,
Did not understand a lot of what you meant in the post but what I understand is, your test class is ignoring the custom setting. It is because it cannot see the setting. 

Custom Settings are "Data" and as such are not part of the test-environment as of Spring '12. 

There are two ways you can fix this issue

1 insert new CustomSetting__c(Field__c = 'Value');

2. Or you could add the following annotation to your testmethod: (see http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm)
 
@isTest(SeeAllData=true)
public static testMethod void anAwesomeTestMethod() {

     ...

 }

 
This was selected as the best answer
Sforce.NinjaSforce.Ninja
Also there has to be a disclaimer to the setting method. 
Disclaimer: I won't recommend you do that.

Mark the answer as best if it solves your Problem.

Sid,
Sforce.Ninja
Ben.SchauerhamerBen.Schauerhamer
Thank you for the reply Sid, adding the insert for the Custom Setting solved the issue.
Sforce.NinjaSforce.Ninja
Brilliant!
PraeneiPraenei
Just adding my own experience to this for future reference.  For our custom setting we were retrieving a boolean value in a validation rule on Account.  When the test class ran the validation rule was running but seemed to return null from the custom setting (& therefore causing the validation rule to fail) despite the test class setting the value correctly.  Eventually found that if we had debug turned on for the user running the test class we got the error.  Debug off & it worked fine.  Very much a case of Schrodinger's cat!