+ Start a Discussion
Mike @ BlackTabMike @ BlackTab 

DML Update in Test Method Doesn't Work

I have a strange problem where a DML update to a record doesn't actually update the record. Here's an example:

 

Test Method:

 

@isTest(SeeAllData=true)
    static void TestChatterAlert(){
    	Test.startTest();
    	User u = [SELECT id, Default_Chatter_Group__c FROM User LIMIT 1];
    	u.Default_Chatter_Group__c = 'Professional Services';
    	update u;
    	
    	Account a = new Account(Name = 'Test Account');
    	insert a;
    	
    	Opportunity o = new Opportunity(
    		Name = 'Test Opportunity',
    		AccountId = a.id,
    		StageName = 'Pre-Opportunity',
    		Amount = 500,
            Chatter_Created__c = false,
    		CloseDate = Date.today(),
    		OwnerId = u.id
    	);
    	insert o;

    	Opportunity opp = [SELECT id, StageName FROM Opportunity WHERE Id = :o.id LIMIT 1];
    	opp.StageName = 'Information Gathering';
        opp.Chatter_Created__c = false;
    	update opp;
    	Test.stopTest();
    }

 

Code To Cover:

 

if((Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert)) || Test.isRunningTest()){
		If(TriggerMonitor.ExecutedTriggers.contains('OpportunityChatter')) return;
		TriggerMonitor.ExecutedTriggers.add('OpportunityChatter');
		
		//Opportunity Chatter Trigger
		for(Opportunity o : Trigger.New){
			if(Trigger.isUpdate){
				system.debug('new stage name: ' + o.stagename + ' old stage name: ' + Trigger.oldMap.get(o.id).StageName);
				if((o.StageName == 'Information Gathering' && Trigger.oldMap.get(o.id).StageName == 'Pre-Opportunity' && o.Chatter_Created__c != true) || Test.isRunningTest()){
					//Get owner information
					User u = [SELECT id, Firstname, Lastname, Default_Chatter_Group__c FROM User WHERE id = :o.OwnerId LIMIT 1];
					//Get Account Information
					Account a = [SELECT id, Account_Type__c, BillingCity, BillingState FROM Account WHERE id = :o.AccountId LIMIT 1];
					
					if(u.Default_Chatter_Group__c != null){
						//Find Chatter Group
						List<CollaborationGroup> cgs = [SELECT id, name FROM CollaborationGroup WHERE name = :u.Default_Chatter_Group__c LIMIT 1];
						
						//Post to Chatter Group
						if(cgs.size() > 0){
							FeedItem post = new FeedItem();
							post.ParentId = cgs[0].id;
							post.Body = u.Firstname + ' ' + u.Lastname + ' has uncovered another Qualified Opportunity for a ' + a.Account_Type__c + ' in ' + a.BillingCity + ',' + a.BillingState + '\n\n' +
							            'Equipment Detail/Purpose:' + o.Equipment_Detail_Purpose__c + '\n' +
							            'Amount: $' + o.Amount + '\n' +
							            'Term: ' + o.Term__c + '\n' +
							            'Likely Structure: ' + o.Likely_Structure__c + '\n' +
							            'Estimated Close Date: ' + o.CloseDate.format();
							insert post;
							
							//Check the Chatter_Created__c on the related opp
							Opportunity oppToUpdate = new Opportunity(id=o.id, Chatter_Created__c = true);
							update oppToUpdate;
						}
					}
				}

 In the trigger, the 5th line that checks for Trigger.IsUpdate() is not covered by the test class. It just doesn't make any sense. 

 

Any help would be great. Thanks

Abhi_TripathiAbhi_Tripathi

Its because of you are not updating Opportunity in your test class, you have only inserted an opportunity,  update your opportunity, then see

 

Hope this works

 

Mike @ BlackTabMike @ BlackTab
In the second to last line of the test class i'm running an update:

update opp;

Is that not sufficient?
Abhi_TripathiAbhi_Tripathi

you are not inserting test record for your "CollaborationGroup", create a test record Insert it and then run your test class

 

Hope this time it works

 

 

Mike @ BlackTabMike @ BlackTab
I put in a couple debug statements to see where the logic is getting stuck, and it's not getting to the:

system.debug('new stage name: ' + o.stagename + ' old stage name: ' + Trigger.oldMap.get(o.id).StageName);

It's stopping right before the Trigger.isUpdate if statement. It's almost like salesforce is ignoring the Update opp; statement and just inserting the opportunity record.

I'm wondering if it makes sense to do something like this:

1.) Start the test
2.) Insert the opportunity
3.) Stop the test
4.) Change the opportunity stage
5.) Start the test
6.) Update the opp
7.) stop the test again.
Abhi_TripathiAbhi_Tripathi

you are doing everything correct

 

but  you have updated your Opportunity just below the query of CollaborationGroup , so controller cant find the any record as it is not inserted in your test class,

 

When you insert records of CollaborationGroup , it will update the your Opportunity in test class.

 

As now code breaks at the query of your Collaboration object.

Abhi_TripathiAbhi_Tripathi

I don't know about the required class of CollaborationGroup record

 

just use this test class

 

@isTest(SeeAllData=true)
static void TestChatterAlert(){

Test.startTest();
User u = [SELECT id, Default_Chatter_Group__c FROM User LIMIT 1];
u.Default_Chatter_Group__c = 'Professional Services';
update u;

Account a = new Account(Name = 'Test Account');
insert a;

Opportunity o = new Opportunity(
Name = 'Test Opportunity',
AccountId = a.id,
StageName = 'Pre-Opportunity',
Amount = 500,
Chatter_Created__c = false,
CloseDate = Date.today(),
OwnerId = u.id
);
insert o;

CollaborationGroup cgs = new CollaborationGroup( name = u.Default_Chatter_Group__c );
insert cgs;

Opportunity opp = [SELECT id, StageName FROM Opportunity WHERE Id = :o.id LIMIT 1];
opp.StageName = 'Information Gathering';
opp.Chatter_Created__c = false;
update opp;
Test.stopTest();
}

Mike @ BlackTabMike @ BlackTab

The following lines still aren't being covered (in red):

 

Image

Abhi_TripathiAbhi_Tripathi

That because record you have inserted in test class doen't match the criteria of the record going to used in trigger,

 

So as you can see

if(o.stageName == "blablah")

 

this is not covered, so you need to insert this value too in the field or Opportunity object.

 

After that test class will be cover.

 

 

Mike @ BlackTabMike @ BlackTab
How is the criteria not being covered? The logic executes when the stage changes from Pre-Opportunity -> Information Gathering, and when the Chatter_Created__c checkbox = false (which i'm modeling in the test class)
Mike @ BlackTabMike @ BlackTab

Any update on a solution for that? I'd like to get this resolved as soon as I can. Thanks