+ Start a Discussion
Adam DrisselAdam Drissel 

System.assertEquals not producing desired result

Here is my code:

Main Class
public class GetOpptyOwnerMgrEmailHandler{
    
    public List<Id> opptyIds {get;set;}
    public List<Opportunity> allOpptys {get;set;}

    public void getMgrEmail(List<Opportunity> opptys){
        
        /*
         * Set variables
         */
        
        getEligibleOpptys(opptys);
        getAllOpptys(opptyIds);
        updateAllOpptys(allOpptys);
    }
    
    private void getEligibleOpptys(List<Opportunity> opptys){
        
        opptyIds = new List<Id>();
        
        for(Opportunity oppty : opptys){
            
            if(oppty.Managers_Email_Address__c == null){
                opptyIds.add(oppty.Id);
            }
        }
    }
    
    private void getAllOpptys(List<Id> opptyIds){
        
        allOpptys = [SELECT Id,Owner.Manager.Email,Owner.Manager.Id FROM Opportunity WHERE Id IN :opptyIds];
    }
    
    private void updateAllOpptys(List<Opportunity> allOpptys){
        
        for(Opportunity oppty : allOpptys){
            
            oppty.Managers_Email_Address__c = oppty.Owner.Manager.Email;
            update oppty;

            system.debug('------------------> here is oppty.Managers_Email_Address__c value: '+oppty.Managers_Email_Address__c);
        }
    }
}

Test Class
@isTest
public class GetOpptyOwnerMgrEmailHandlerTest {
        
    /*
    * Method to test "GetOpptyOwnerMgrEmail" trigger
    */

    @isTest
    public static void testGetOpptyOwnerMgrEmail(){
        
        /*
         * Create two test Users, one manager, one standard
         */
        
        User mgr = DataFactory.createUserMgr();
        insert mgr;
        User u = DataFactory.createUser(); 
        u.ManagerId = mgr.Id;
        insert u;
        
        System.assertEquals(u.ManagerId,mgr.Id,'The managerId didn\'t set properly');
        
        /*
         * Create test Opportunity
         */
        
        Opportunity opp = DataFactory.createOpp();
        opp.Type = 'New Account';
        opp.StageName = 'Prospecting';
        opp.OwnerId = u.Id;
        opp.Competitors__c = 'SaaS Corporate';
        opp.Current_Vendor__c = 'SaaS Corporate';
        opp.Close_Reason__c = 'Features';
        
        System.RunAs(u){
            
            insert opp;
        
            system.debug('------------------> here is the opp value: '+opp);
            system.debug('------------------> here is opp.Managers_Email_Address__c value: '+opp.Managers_Email_Address__c);

            System.assertEquals(opp.Managers_Email_Address__c,mgr.Email,'The email addresses aren\'t the same');
        }
    }
}

DataFactory Class
public class DataFactory {

    /*
     * Method to create test Standard User
     */
    
    public static User createUser(){
        
        Id proId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1].Id;
        UserRole ur = createUR();
        insert ur;
        User u = new User(
            alias = 'u1', 
            email = 'testUser@company.com',
            emailencodingkey = 'UTF-8',
            firstname = 'Test',
            lastname='User',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles',
            profileId = proId,
            isActive = true,
            username = 'testUser@company.com',
            userRoleId = ur.id
        );
        return u;
    }

    /*
     * Method to create test User Manager
     */
    
    public static User createUserMgr(){
        
        Id proId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1].Id;
        UserRole ur = createUR();
        insert ur;
        User u = new User(
            alias = 'uMgr', 
            email = 'testUserMgr@company.com',
            emailencodingkey = 'UTF-8',
            firstname = 'Test',
            lastname='UserMgr',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles',
            profileId = proId,
            isActive = true,
            username = 'testUserMgr@company.com',
            userRoleId = ur.Id
        );
        return u;
    }

    /*
     * Method to create UserRole
     */
    
    public static UserRole createUR(){
    
        // Method creates a UserRole that will cause the GEO to evaluate to "NA"
        
        UserRole ur = new UserRole();
        ur.Name = 'NA TestRole';
        ur.OpportunityAccessForAccountOwner = 'None';        
        return ur;
    }

    /*
     * Method to create test Opportunity
     */
    
    public static Opportunity createOpp(){
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.CurrencyIsoCode = 'USD';
        opp.CloseDate = date.parse('1/1/2199');
        opp.Managers_Email_Address__c = null;
        return opp;
    }
}

My System.assertEquals is evaluating to false.  When I debug the value at the very end of the main class it is set properly.  But, it isn't populating to the opp variable in the test class.  What am I missing here?


 
Best Answer chosen by Adam Drissel
Ajay Nagar 7Ajay Nagar 7
Hi Adam,
Opportunity opp2=[select Managers_Email_Address__c from opportunity where Id =:opp.Id];
System.assertEquals(opp2.Managers_Email_Address__c,mgr.Email,'The email addresses aren\'t the same');
Use this code for comparison.
 

All Answers

Ajay Nagar 7Ajay Nagar 7
Hi Adam,
Opportunity opp2=[select Managers_Email_Address__c from opportunity where Id =:opp.Id];
System.assertEquals(opp2.Managers_Email_Address__c,mgr.Email,'The email addresses aren\'t the same');
Use this code for comparison.
 
This was selected as the best answer
surasura
from what I can see you havent called your mainclass methods in your test class which assign manager email address to your opportunity record's Managers_Email_Address__c field
 
@isTest
public class GetOpptyOwnerMgrEmailHandlerTest {
        
    /*
    * Method to test "GetOpptyOwnerMgrEmail" trigger
    */

    @isTest
    public static void testGetOpptyOwnerMgrEmail(){
        
        /*
         * Create two test Users, one manager, one standard
         */
        
        User mgr = DataFactory.createUserMgr();
        insert mgr;
        User u = DataFactory.createUser(); 
        u.ManagerId = mgr.Id;
        insert u;
        
        System.assertEquals(u.ManagerId,mgr.Id,'The managerId didn\'t set properly');
        
        /*
         * Create test Opportunity
         */
        
        Opportunity opp = DataFactory.createOpp();
        opp.Type = 'New Account';
        opp.StageName = 'Prospecting';
        opp.OwnerId = u.Id;
        opp.Competitors__c = 'SaaS Corporate';
        opp.Current_Vendor__c = 'SaaS Corporate';
        opp.Close_Reason__c = 'Features';
        
        System.RunAs(u){
            List<Opportunity> opps = new List<Opportunity >();
            insert opp;
            opps.add(opp);
            
            GetOpptyOwnerMgrEmailHandler GOEH = new GetOpptyOwnerMgrEmailHandler();
            GOEH.getMgrEmail(opps);

            system.debug('------------------> here is the opp value: '+opp);
            system.debug('------------------> here is opp.Managers_Email_Address__c value: '+opp.Managers_Email_Address__c);

            System.assertEquals(opp.Managers_Email_Address__c,mgr.Email,'The email addresses aren\'t the same');
        }
    }
}

 
Adam DrisselAdam Drissel
Thanks Ajay!  Worked like a charm.  Who knew you had to call the data again, right?!  ;)