Find your content:

Search form

You are here

Help with a test for a trigger

 
Share

The code coverage is good, and the trigger works when testing it manually, however, my system.assertequals statements are failing. the finAcc1.wealth_advisor_lookup__c (and the others) is null. Any guidance would be appreciated. Thanks!

NOTE: Financial_Account__c is a child in a master-detail with Account (also called Client in the code). Also, we use Person Accounts. Thanks again!

Trigger:

trigger transferRelationshipTeamFromAcct on Financial_Account__c (before insert, before update) {
    FinancialAccountManager.handleUpdateTransferRelationshipTeamFromAcct(trigger.new);
}

Class:

public class FinancialAccountManager {
public static void handleUpdateTransferRelationshipTeamFromAcct(List<Financial_Account__c> FinAcctsTrNew){
    Set<Id> clients = new Set<Id>{};
    for(Financial_Account__c finAcct : finAcctsTrNew){
        //Compile a set of all clients from all FinAccts in Trigger.new
        clients.add(finAcct.client__c);
    }

    //Get the client's fields
    Map<Id, Account> clientsMap = new Map<Id, Account>([
        SELECT Id, Wealth_Advisor__c, Portfolio_Manager_Lookup__c,Trust_Officer__c, Other_Team_Member_1__c, Other_Team_Member_2__c
    FROM Account
    WHERE Id IN :clients]);

    for(Financial_Account__c finAcct : FinAcctsTrNew){
        finAcct.Wealth_Advisor_Lookup__c = clientsMap.get(finAcct.client__c).Wealth_Advisor__c;

        finAcct.Portfolio_Manager_Lookup__c = clientsMap.get(finAcct.client__c).Portfolio_Manager_Lookup__c;

        finAcct.Trust_Officer_Lookup__c = clientsMap.get(finAcct.client__c).Trust_Officer__c;

        finAcct.Other_Team_Member_1_Lookup__c = clientsMap.get(finAcct.client__c).Other_Team_Member_1__c;

        finAcct.Other_Team_Member_2_Lookup__c = clientsMap.get(finAcct.client__c).Other_Team_Member_2__c;
    }
}
}

Test:

@isTest
private class testTransferRelationshipTeamFromAcct{
    static testMethod void testUpdateFinAcctToTransferRelationshipTeamFromAcct(){
    ////Query RecordType Id to Create PersonAccounts for Test////
    Id RecId = [
        SELECT r.Id, r.Name, r.DeveloperName, r.IsPersonType 
        FROM RecordType r 
        WHERE sObjectType = 'Account' AND IsPersonType=True AND DeveloperName='Individual'
    ].Id;

    system.debug('RecordId:' + RecId);

    Id AckId = [
        SELECT u.Id, u.Name
        FROM User u
        WHERE name = 'Jason Ackerman'
    ].Id;

    Id FlaksId = [
        SELECT u.Id, u.Name
        FROM User u
        WHERE name = 'Zoie Flaks'
    ].Id;

    Account acc1 = new account( salutation = 'Mr.', firstname = 'John', lastname = 'Sandbox1', Wealth_Advisor__c = AckId, Trust_Officer__c = AckId, Other_Team_Member_1__c = AckId, Other_Team_Member_2__c = AckId, Portfolio_Manager_Lookup__c=AckId , recordtypeid = RecId );

    Account acc2 = new account( salutation = 'Mr.', firstname = 'John', lastname = 'Sandbox1', Wealth_Advisor__c = AckId, Trust_Officer__c = AckId, Other_Team_Member_1__c = AckId, Other_Team_Member_2__c = AckId, Portfolio_Manager_Lookup__c=AckId , recordtypeid = RecId );

    insert acc1;
    insert acc2;

    Financial_Account__c finAcc1 = new Financial_Account__c(Name='Test Account 1', client__c = acc1.id);
    Financial_Account__c finAcc2 = new Financial_Account__c(Name='Test Account 1', client__c = acc2.id);

    system.debug('MINE: does client on fin acct: ' + finAcc1.client__c + ' ?= ' + acc1.id);
    system.debug('MINE: does WA on fin acct: ' + finAcc1.wealth_advisor_lookup__c + ' ?= ' + acc1.wealth_advisor__c);

    test.starttest();
    insert finAcc1;
    insert finAcc2;

    acc1.Wealth_Advisor__c = FlaksId;
    finAcc1.Financial_Account_Long_Title__c = 'test again';
    update finAcc1;
    test.stoptest();

    system.debug('MINE: does WA on fin acct: ' + finAcc1.wealth_advisor_lookup__c + ' ?= ' + acc1.wealth_advisor__c);

    System.AssertEquals(finAcc1.wealth_advisor_lookup__c, acc1.wealth_advisor__c);
    System.AssertEquals(finAcc1.portfolio_manager_lookup__c, acc1.portfolio_manager_lookup__c);
    System.AssertEquals(finAcc1.trust_officer_lookup__c, acc1.trust_officer__c);
    System.AssertEquals(finAcc1.other_team_member_1_lookup__c, acc1.other_team_member_1__c);
    System.AssertEquals(finAcc1.other_team_member_2_lookup__c, acc1.other_team_member_2__c);

}
}

Attribution to: jackerman09

Possible Suggestion/Solution #1

You need to re-query the Financial_Account__c before assertion.

update finAcc1;
test.stoptest();
finAcc1 = [
    Select Id, Name, wealth_advisor_lookup__c, portfolio_manager__c, other_team_member_1_Lookup__c, other_team_member_2_lookup__c 
    FROM Financial_Account__c
    where Id = :finAcc1.Id
];

//then do assertions

As an aside, it is (probably) best practice to create Users rather than query them. AckId and FlaksId hopefully are returning valid user ids. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm


Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2050

My Block Status

My Block Content