You need to sign in to do that
Don't have an account?

Test Class for trigger matching Opportunity's Agent ID (Custom Object ID) with Account's Agent ID
Hi,
Need help with getting the test class to 100% as I'm having trouble with a couple of lines which may help with future testing.
1.) Trigger on Opportunity
trigger SetAgent on Opportunity (before insert, before update) { List<String> accountIds = new List<String>(); List<Account> parentAccounts = new List<Account>(); for(Opportunity oppty : Trigger.new) { accountIds.add(oppty.AccountId); } parentAccounts = [Select Id, Agent__c from Account where Id in :accountIds]; Map<String,Account> accountMap = new Map<String,Account>(); for(Account a : parentAccounts) { accountMap.put(a.Id,a); } for(Opportunity oppty : Trigger.new) { Account parentAccount = accountMap.get(oppty.AccountId); if(parentAccount != null) { oppty.Agent__c = parentAccount.Agent__c; } } }
2.) Apex Test Class
@isTest private class SetAgentTest { static testMethod void SetAgent() { Account a = new Account(Id = '001K000000KnlcE', Agent__c = 'a03K0000001A7v2'); Opportunity o = new Opportunity(Name = 'TestOpportunity', StageName = 'Prospecting', CloseDate = system.Today(), Agent__c = a.Agent__c); insert o; System.assertEquals(a.Agent__c, o.Agent__c); } }
3.) The 83% result tells me that these two lines from the code above were not tested, a brief explanation of why would be much appreciated.
accountMap.put(a.Id,a);
oppty.Agent__c = parentAccount.Agent__c;
Thanks again!
Hi,
In your test class, i think it is ideally not to assign the opportunity agent explicitly, because when the opportunity is inserted, the agent will be copied over from account by the trigger, and that is exactly the thing you want to test in the apex test class.
So if the assertion passed then your trigger should be working as expected.
Btw, you can assign query result to a map directly:
Regards,
Hengky
All Answers
insert your acc, and add to opp.
Hi,
In your test class, i think it is ideally not to assign the opportunity agent explicitly, because when the opportunity is inserted, the agent will be copied over from account by the trigger, and that is exactly the thing you want to test in the apex test class.
So if the assertion passed then your trigger should be working as expected.
Btw, you can assign query result to a map directly:
Regards,
Hengky
Tried yours DuTom, but ended up with 0%
Appreciate your insight though!
Hengky,
You're a coding wizardl!
Revising my trigger to this one allowed my simple test class to reach 100%, which also goes for you, sheer greatness!
Regards,
PFang