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
Robert Robinson 48Robert Robinson 48 

Apex test class for Apex Trigger

I am having a major brain block. I have the following Apex Trigger designed to keep a Probability % at a previously (manually) set level. This is that code:
trigger keepProbability on Opportunity (before update) {
    for(Integer i = 0; i < Trigger.new.size(); i++) {
        if(Trigger.old[i].stagename != Trigger.new[i].stagename) {
            if(Trigger.old[i].probability != Trigger.new[i].probability) {
               Trigger.new[i].probability = Trigger.old[i].probability;
            }
        }
    }

}

The trigger works great, but I am losing my mind trying to write a Test Class. Any tips on writing the test would be greatly appreciated.
 
Best Answer chosen by Robert Robinson 48
sachinarorasfsachinarorasf
Hi Robert,

I have gone through your problem.

Here is the code as per the requirements And you can use it. 
 
@isTest
private class keepProbability_Test  {
   public static testMethod void testSaveProbability(){

   Opportunity opportunityObject = new Opportunity ();
   opportunityObject.name = 'test';
   opportunityObject.stagename = 'Qualification';
   opportunityObject.CloseDate = date.today();
   insert opportunityObject;
   
   Test.startTest();
   opportunityObject.Probability = 70;
   opportunityObject.stagename = 'Proposal'; 
   update opportunityObject;
   Test.stopTest();
   }


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora

All Answers

Christan G 4Christan G 4

Hi Robert, I hope you are well. As a best practice, you should separate the logic of your code into a separate apex class and have your apex trigger call out to your apex class. It will make testing a lot easier. 

Regarding suggestions for creating a test class. You basically have to create 200 new opportunity records with stagename and probability values. The reason I stated 200 new records is this will ensure that your code is bulkified. After inserting these new opportunity records. Iterate through the new opportunities and change the stagename and probability to a new value. Store these changes in a new list. When using the DML update language, surround it with System.Test.startTest() and System.test.stopTest() methods. Finally, use system assert methods to ensure the updated opportunities reflect the results you were looking for. 

Robert Robinson 48Robert Robinson 48
Thank you. I am a bit lost on how to iterate through the opportunites created by the trigger.
Robert Robinson 48Robert Robinson 48
Ok. I am starting simply; will bulkify the code as suggested later. I have created the test (shown below), but and getting 0 code coverage so far. Aside from the lack of bulkification code, what flaws arein this test (be brutal, I can take it to learn)...

@isTest
private class testkeepProbability {
   public static testMethod void testSaveProbability(){
//Create the Opportunity Record
   Opportunity a = new Opportunity ();
   a.name = 'test';
   a.stagename = 'Qualification';
   insert a;
//Change the Probability of the Opportunity
   a.Probability = 25;
   upsert a;
//Change the Stage of the Opportunity and test Probability
   a.stagename = 'Proposal'; 
   upsert a;
   System.assertEquals(25, a.Probability);
   }
sachinarorasfsachinarorasf
Hi Robert,

I have gone through your problem.

Here is the code as per the requirements And you can use it. 
 
@isTest
private class keepProbability_Test  {
   public static testMethod void testSaveProbability(){

   Opportunity opportunityObject = new Opportunity ();
   opportunityObject.name = 'test';
   opportunityObject.stagename = 'Qualification';
   opportunityObject.CloseDate = date.today();
   insert opportunityObject;
   
   Test.startTest();
   opportunityObject.Probability = 70;
   opportunityObject.stagename = 'Proposal'; 
   update opportunityObject;
   Test.stopTest();
   }


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
This was selected as the best answer
Robert Robinson 48Robert Robinson 48
I resolved this Friday night; my apologies for not updating this thread. Here is my solution, with a twist on Sachin's. I wanted to test that the Probability kept its value after it was changed, then the stage was changed. Otherwise, same deal:

@isTest
private class testkeepProbability {
   public static testMethod void testSaveProbability(){
//Create the Opportunity Record
   Opportunity a = new Opportunity();
   a.CloseDate = system.today();
   a.name = 'test Opp';
   a.stagename = 'Qualification';
   insert a;
 //Change the Probability and test when Stage is changed  
   a.Probability = 25;
   update a;
   Test.startTest();   
   a.stagename = 'Needs Analysis (ODF)'; 
   update a;
   Test.stopTest();
   System.assertEquals(25, a.Probability);
   }
}


Thank you for the solution, Sachin. I will mark it as best answer.