Difference between deploying user and System.runAs()

I have a weird situation where some test classes will deploy to Production only if the user deploying it has a specific keyword on their LastName – “TestMode, TestsEnabled”.

Updating my test classes to create a new user and use that in System.runAs() did not work.

The test class is for another apex class which uses packaged components from Informatica CC360 (installed package). The test class does NOT use @SeeAllData annotation and creates it own data, custom settings data from static resources or generated by code. This test class deployed successfully in all sandboxes from Dev through (but excluding) Production, including intermediate staging sandboxes but failed in Production. Looking at the debug log, it fails inside one of the packaged classes that eventually get called from my test methods.

Informatica’s solution was to update the deploying user’s LastName as described above and it magically works! They’re currently being an extreme pain in the neck about giving more clarity on how or why this works.

Question: I know System.runAs() does not apply object/field level permissions but only record level. However, could Informatica have built something based on the current user – which somehow is the current deploying user and NOT the System.runAs() user?

EDIT: Adding some code snippets to ensure my System.runAs code is correct.

User cc360TestUser = createCC360TestUser();
SearchBeforeCreate.SearchResultCustomer searchResult;
System.runAs(cc360TestUser) {
    searchResult = SearchBeforeCreate.searchCustomerWithCC360(customerToken);
// Asserts..

//Any CC360 activity happens within the SearchBeforeCreate line
//createCC360TestUser is a static method in the same class
static User createCC360TestUser() {
    Profile sysAdminProfile = [SELECT Id, Name FROM Profile WHERE Name = 'System Administrator'];
    User cc360TestUser = new User(ProfileId = sysAdminProfile.Id, Username = 'cc360testuser@example.com', Email = 'cc360admin@example.com', EmailEncodingKey = 'UTF-8', LocaleSidKey = 'en_US', LanguageLocaleKey = 'en_US', TimeZoneSidKey = 'America/New_York', Alias = 'cc360', Lastname='CC360 TestMode, TestsEnabled');
    insert cc360TestUser;

    return cc360TestUser; 


Source : Link , Question Author : Vid L , Answer Author : Community

Leave a Comment