+ Start a Discussion
John NeilanJohn Neilan 

Test Class for Opportunity Update

I have a test class where I am trying to cover code on an update trigger when the Effective Date or Stage is changed. However, when I run the test, none of the Opportunity data gets changed. Do I have the proper syntax in my test to test for a change to the Opportunity?
static testmethod void testAcctData(){

    Account acct5 = TestCreateRecords.createAcct(0);
    insert acct5;

    Opportunity opp1 = TestCreateRecords.createOppNew(acct5.Id);
    insert opp1;

    Opportunity opp1a = [SELECT Id,Effective_Date__c,StageName
                        FROM Opportunity
                        WHERE Id =: opp1.Id];
        opp1a.Effective_Date__c = Date.TODAY().addDays(5);
        opp1a.StageName = 'Phase 5: Closed Won';
        UPDATE opp1a;


Priya GovindasamyPriya Govindasamy
Your clode looks fine. Make sure the Trigger is Active.
John NeilanJohn Neilan
Yes, the trigger is active.
Cyrus TalladenCyrus Talladen
Test classes does not persist data after they are finished running.  This is why you have to use debug lines to print out if the test opportunity you have in your test class has actually executed and was tested.  Test classes are made so that your code is covered so that they can be pushed for deployment.  Also, in your code you are inserting opp1 but then you have updated opp1a.  this should have thrown an error because opp1a should exist first.

Cyrus T
Salesforce engineer