function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marc G.ax1040Marc G.ax1040 

Test class assertion error, Plz Help !

Hi , 

 

I need help in fixing the assertion issue in this test class. I have 2 objects with Start_Term__c fields on both  Opportuntiy and Contact respectively . My trigger basicallly updates the Start_Term__c  field on contact.

 ***************************************************************************************/

@isTest 
private class opportunity_syncStartTermTest
{
	
         static testmethod void test()
         {
          Contact newcontact = new Contact(LastName = 'abc',
	 			           Start_Term__c = 'Jan 2012'
	 				   );
	    insert newcontact;
	 Opportunity newopportunity = new Opportunity( Contact__c = newcontact.Id,
	  						Name = 'abc',
		         				StageName = 'StageName',
							Start_Term__c = 'Jan 2012',
							CloseDate = Date.today()
							 );
          insert newopportunity;
		   Test.startTest();
            newopportunity.Start_Term__c = 'Sep 2011';
            update newopportunity; 
           Test.stopTest();
            Contact[] message = [SELECT Id, Start_Term__c,Program_Primary__c FROM Contact WHERE ID = :newcontact.Id limit 1]; 
            if(message.size() > 0)
            {
                System.assert(message[0].Start_Term__c == 'Sep 2011');  
             }
       
        }
      
}


}

 Thanks for your help

Ritesh AswaneyRitesh Aswaney

Your test class seems okay. Perhaps your trigger needs a second look. You're not by any chance rejecting a month in the past, i.e. Sep 2011.

Marc G.ax1040Marc G.ax1040

HI Ritesh,

 

I tested it with Sep 2014 , it is not working. The code is working perfect too only thing it's failing is assertion

Mike@COLMike@COL

Chances are your trigger isn't doing what you think it is. Try using assertequals so you can see that the value of the message[0].Start_Term__c is. I bet it that it is still 'Jan 2012'.

 

Best,

Mike

Marc G.ax1040Marc G.ax1040

HI Mike, 

 

Yes, the value is still Jan 2012 after using assert equal but i don't understand why is the trigger working and the assertion not . In the UI I don't have any problem updating the record on other object.. Please help 

Mike@COLMike@COL

There could be a lof of reasons.  Add some system.debug statements to your trigger.Take a look at the debug log that is generated when you run your unit test. Verify that the trigger is running as you expect.

Marc G.ax1040Marc G.ax1040
Thanks Mike, i did as suggested but still have the same problem.
@isTest 
private class opportunity_syncStartTermTest
{
	
         static testmethod void test()
         {
          Account account = new Account();
				           account.Name = 'cde';
		  insert account;
		  
		  Program__c program = new Program__c();
        							program.Name = 'MIB-US-Boston';
          insert program;
          Contact newcontact = new Contact(LastName = 'abc',
          								   AccountID = account.Id,
          								   FirstName = 'FirstName',
	 								       Start_Term__c = 'Jan 2011'
	 								  );
	      insert newcontact;
	      System.debug('opportunity_syncStartTermTest::test::newcontact.ID==>' +newcontact.ID);
	      Opportunity newopportunity = new Opportunity( Contact__c = newcontact.Id,
	  												Name = 'abc',
		         							 		StageName = 'StageName',
										            Start_Term__c = 'Jan 2011',
										         	CloseDate = Date.today(),
										         	AccountId = account.Id,
										         	Program__c = program.ID
										         	
										          );
		    insert newopportunity;
		    System.debug('opportunity_syncStartTermTest::test::newopportunity.ID==>' +newopportunity.ID);
		    System.debug('opportunity_syncStartTermTest::test::newopportunity.Start_Term__c==>' +newopportunity.Start_Term__c);
            newopportunity.Start_Term__c = 'Jan 2013';
            Test.startTest();
            update newopportunity; 
            Test.stopTest();
            
            Opportunity[] messages = [SELECT Id, Start_Term__c FROM Opportunity WHERE ID = :newopportunity.Id limit 1];
	            System.debug('opportunity_syncStartTermTest::test::messages.ID==>' +messages[0].ID);
	            System.debug('opportunity_syncStartTermTest::test::messages.Start_Term__c==>' +messages[0].Start_Term__c);
            
            Opportunity[] oppmsg = [SELECT Contact__c FROM Opportunity WHERE ID = :newopportunity.ID limit 1];
              System.debug('opportunity_syncStartTermTest::test::oppmsg[0].ID==>' +oppmsg[0].Contact__c);
            
            Contact[] message = [SELECT Id, Start_Term__c,Program_Primary__c FROM Contact WHERE ID = :newcontact.ID limit 1];
     //       System.debug('opportunity_syncStartTermTest::test::message[0].ID==>' +message[0].ID);
     //       System.debug('opportunity_syncStartTermTest::test::message[0].Start_Term__c==>' +message[0].Start_Term__c);
    //        System.debug('opportunity_syncStartTermTest::test::message[0].size==>' +message.size()); 
            if(message.size() > 0)
            {
                System.assert(message[0].Start_Term__c == 'Jan 2013');  
                System.debug('opportunity_syncStartTermTest::test::message[0].Start_Term__c==>' +message[0].Start_Term__c);
             }
        
        }
       
}

 

crop1645crop1645

I echo Mike's comments -- you need debug statements in the Trigger code, not just in the testmethod.  This way you can rule out whether there is some subtle difference in how your trigger responds in a UI scenario versus a testmethod