2
trigger trig_Opportunity_CreateOppOwner on Opportunity (before update, after insert) {

List<id>OppsID = new List<id>(); //List that will hold all id of all new Opportunities owners

for(Opportunity Opp:Trigger.new){ //Loop that only retrives new versions of the Opportunity which have new owners.
    if (trigger.oldMap.get(Opp.id).Op_Owner__c != trigger.oldMap.get(Opp.id).OwnerId)
        OppsId.add(Opp.ID);
}
List<Opportunity> oppsFromDb =  [SELECT id, OwnerId FROM Opportunity WHERE id IN:OppsID];   
for(Opportunity useOpps:oppsFromDb){    //all new Owners are then listen in the Op_Owner__c field.                      
    trigger.newMap.get(useOpps.Id).Op_Owner__c = useOpps.OwnerId;
}   

So The above is my trigger. Basically the triggers checks to see if all new Opportunities have new owners, if it does, then we make sure to add the ownerid to a new custom field called op_owner__c. This way we can keep track of all new owners of new Opportunities.

I need to create a test for this trigger class. Here is my attempt at it so far but it's incomplete, I'm unsure what to put into the test class, aka how many opportunities I'll need along with what I should put into the WHERE clause in the opportunity.

public with sharing class Test_Trig_Opportunity_CreateOppOwner {
static testMethod void testTriggerOpp(){

    //initialize test variables (called from TestUtil class)         
Contact testContact = TestUtil.initContact();
Account testAccount = TestUtil.initAccount();        
    Opportunity testOpp = TestUtil.initOpportunity(testAccount,testContact);         


    Test.startTest();
    system.debug('Test is starting');

    Opportunity oppy = [SELECT id, OwnerId FROM Opportunity  WHERE];

    system.debug('end test');
Test.stopTest();// Test ended
}

This is what the testutil more or less looks like for the others as well.

public static Account initAccount(){
/**
    *   Create an Account
    *   Insert Account into DB
    *   Return Account back into testPage in order to use information
**/     
    Account testAccount = new Account (
                name = 'JFTestAccount' , type = 'Potential Customer' , ShippingState= 'Alabama', ShippingCountry= 'United States', BillingState= 'Alabama',BillingCountry= 'United States'      
                );
    insert testAccount;
    return testAccount;
}
5
  • 1
    Minor point, but the first thing is that a test class starts with @isTest on the 1st line.
    – crmprogdev
    Jun 4, 2013 at 13:48
  • whoops, forgot to copy paste that into the question. Thanks for pointing it out though.
    – SSadat
    Jun 4, 2013 at 13:51
  • 1
    Do your test util classes just initialize variables or do they insert them too? That's unclear to me. And are you able to specify how many are contacts, etc are initialized?
    – crmprogdev
    Jun 4, 2013 at 13:56
  • The testutil classes initialize my variables along with inserting them as well. For example, initaccount is posted above for clarification.
    – SSadat
    Jun 4, 2013 at 14:01
  • I suggest you "bulkify" your data creation classes to where you can specify how many data instances you want it to create for you by passing an integer to your class.
    – crmprogdev
    Jun 4, 2013 at 14:24

2 Answers 2

1

Okay, I think your test class needs to look something like this:

@isTest
public with sharing class Test_Trig_Opportunity_CreateOppOwner {
static testMethod void testTriggerOpp(){

   //initialize test variables (called from TestUtil class)         
   Contact testContact = TestUtil.initContact();

   Account testAccount = TestUtil.initAccount();


   Test.startTest();
   //Test is starting (The above is not something one debugs)

   Opportunity testOpp = TestUtil.initOpportunity(testAccount,testContact);         

   // this should cause your trigger to fire since it inserts the opps

   Test.stopTest();
   // Test ended

   // now it's time to validate that the trigger worked

   // Opportunity oppy = [SELECT id, OwnerId FROM Opportunity  WHERE];
   // No need to run a query since you already know what the data should look like.

   For(Opportunity o : testOpp){
   system.Assert(o.Op_Owner__c = o.OwnerId, true);
   }
   // the above is set-up for bulk testing
   // ideally, you'd create some records that were already assigned and some that were not
   // to make certain your trigger was working properly to sort the two of them out.
   // to do that, create two sets of contacts, accts, & opps. Initialize 1 set of opps
   // before you start the test. Those opps will already be in the data since 
   // the trigger should run at that time before you start the test. However, make certain
   // you run the above test on that data too before you begin this test! Its like having
   // two test methods on your trigger. Either that or create one set of your opps 
   // with the custom value already filled in.


   }
}

You'll want to double-check the above code as I've written it "on the fly", so minor adjustments may be required to de-bug it for your purposes.

2
  • This helps immensely. What you mentioned about creating two sets of contacts,accts,&opps to test it out fully was my original plan and that's what I'm doing right now. I'm confused as to how to test the first set of data and then test the second set of data as you mentioned above.
    – SSadat
    Jun 4, 2013 at 14:44
  • When I wrote that, I hadn't realized this was a BEFORE trigger. If this were an AFTER trigger, that would be important. Since it's a BEFORE, there's no trigger.old. If it were, you'd want to initialize one set of Opps before going into start.Test and run the system asserts on those Opps if you didn't create them with the OwnerId already assigned to Op_Owner__c. You could use the same contacts and Accounts, but would need a diff sequence of Opp names.
    – crmprogdev
    Jun 4, 2013 at 14:58
2

Before answering the question i am assuming the field "Op_Owner__c" is a custom user field on oppotunity object. Going ahead with that assumption, Since this trigger is executed on "before update and after insert" event, the only thing that you need to do it to insert an opportunity record where Op_Owner__c is not equal to the owner of opportunity record.

therefore in TestUtil class create an opportunity record satisfying the above condition and instantiate it after Test.startTest(). This should do the trick.

Hope this answers your query.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .