2

I'm a newbie to Apex and can't say how much I appreciate this Board. I have a simple Trigger set up to update the Description fields on the Opportunity Object. I need to write a Test to cover the Trigger.

Here's the Trigger:

trigger updateFields on Opportunity (before insert,before update) {

    for(Opportunity opp : Trigger.new){ 
        if(opp.StageName=='Closed as Won'){ 
            (opp.description= 'Assessment Completed!!') ; 
        }
        if(opp.StageName=='Closed as Lost'){ 
            opp.description= 'Opportunity Lost'; 
        }
    }
}

****Here's the Test I attempted to write (I'm looking for feedback on this: ****

@isTest
public class SampleTest {
public static testmethod void MyMethod(){
Test.startTest();

// TO DO: implement unit test
Account account = new Account (Name = 'testa');
insert account;
Opportunity[] opportunitys = new Opportunity[]{

new Opportunity (name = 'test1',
account = account,
stagename = 'Closed As Won',
closedate = system.today(),

new Opportunity (name = 'test2',
account = account,
stagename = 'Closed As Lost',
closedate = system.today()
};
update opportunitys;

Test.stopTest();
}
}

Thanks!

4
  • Hey, welcome back. A few pointers: for standard object relationship fields (such as the Account of an Opportunity), the field you update is "AccountId" or "ManagerID," not "Account" - you set the actual ID of the related SObject to this field. You also don't want to be updating the list opportunitys in this case; you want to be inserting them, since they're records that don't already exist in the database. Commented Nov 17, 2015 at 22:20
  • In terms of testing best practices, some would say you should separate each business case into separate methods (I need to work on this area myself) - in this case, you would separate inserting an Opportunity as Closed Won and Closed Lost into different methods. You should also add assert statements to ensure that the Description value on each of these Opportunities is what you expected. Finally, you not only want to test the happy path - you want to test expected cases, edge cases, and nonsense cases as well :) Commented Nov 17, 2015 at 22:23
  • 1
    Thanks, Brian! I need to get to know Best Practices for sure and this helps. I changed the Insert as you recommended and will try to break up scenarios next time.
    – A Perry
    Commented Nov 17, 2015 at 22:28
  • 1
    in addition to the other good comments and answers, put your constant strings into a CONSTANTS class with getters for each value. E.g. public String OPP_STAGE_CLOSEDWON {get {return 'Closed as Won';}} and refer to the strings in your code and testmethod via the getter as in if(opp.StageName==CONSTANTS.OPP_STAGE_CLOSEDWON) This way, if the picklist values change, you only have to change one class in your code base and you ensure that there are no typos between code and testmethods. In your example, code uses as and testmethod uses As for stage.
    – cropredy
    Commented Nov 18, 2015 at 1:04

1 Answer 1

1

Testing Apex is one of the most important topic that definitely cannot be ignored when doing apex .

There are some documents that will guide you in figuring best pratices

1)Developerforce Article on this is my first reference

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests

2)Video session if you want to learn step by step

https://www.youtube.com/watch?v=n9amswhOxJw

3)TrailHead Module

https://developer.salesforce.com/trailhead/module/apex_testing

4)Ten principles of apex Testing Webinar

https://developer.salesforce.com/events/webinars/Apex_Testing

Feedback on your test code

  1. No asserts found .Please assert your business scenario
  2. No data factory used for creating test data
  3. No system.RunAs used to check with various profiles and Users
  4. Write as many testmethod to cover bulk testing ,positive ,negative scenarios.
0

You must log in to answer this question.

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