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
Dchris222Dchris222 

Creating a test class for an Apex Class

I developed an apex class that I need to test but don't understand how to develop a test class for it. The class creates two lists: all Opportunities and the Opportunity Feed with comments. I then evaluate each list and assign a value to the Activity Status based on date fields in each list.

 

I want to test the apex class to see if it works so I am assuming I need to create a test class. I have read the limited documentation, but still don't understand where to go from here. Help would be appreciated as I am very new at this.

 

Here is my Apex class:

 

 

public class OppPoliceController {
	
  public void Opportunity() {
	
     List<Opportunity> Opptys2 = [SELECT Opportunity_Number__c, LastActivityDate, Activity_Status__c FROM Opportunity ORDER BY LastActivityDate];	
     List<OpportunityFeed> Opptys = [SELECT id, (SELECT CreatedById, CreatedDate FROM FeedComments ORDER BY CreatedDate DESC) FROM OpportunityFeed];
     
 	 
       for(OpportunityFeed o : Opptys) { 
       	 for(Opportunity Op : Opptys2){
          
            if(system.now() > Op.LastActivityDate.addDays(14) || system.now() > o.CreatedDate.addDays(14)) {
              
               string strStatus = 'High';
               Opportunity Opp = New Opportunity(Activity_Status__c = strStatus);
               Opptys2.add(Opp);
                
            }
            
            else if(system.now() >= Op.LastActivityDate.addDays(7) || system.now() < Op.LastActivityDate.addDays(14) 
                     || system.now() >= o.CreatedDate.addDays(7) && system.now() < o.CreatedDate.addDays(14)) {
         
               string strStatus = 'Medium';
               Opportunity Opp = New Opportunity(Activity_Status__c = strStatus);
               Opptys2.add(Opp);
               
            }
            
            else if(system.now() < Op.LastActivityDate.addDays(7) || system.now() < o.CreatedDate.addDays(7)) {
            	
                string strStatus = 'Low';
                Opportunity Opp = New Opportunity(Activity_Status__c = strStatus);
                Opptys2.add(Opp);
               
            }
            else {
            	
            	string strStatus = 'Error';
            	Opportunity Opp = New Opportunity(Activity_Status__c = strStatus);
                Opptys2.add(Opp);
            	
            }
       	 }    
       }
         try {
          upsert Opptys2;
         } catch (DmlException e) {
         	system.debug('Error: ' + e.getMessage());
         }
  }     
}

 

 

Ralph CallawayRalph Callaway

Hi Dchris22,

 

There is a good introduction on the developer force wiki that should help get you started.

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

In general I always try to supplant the documentation with searching across developer force and google.

Dchris222Dchris222

My situation is different from all the example code. My code reads from lists and then evaluates the lists and populates the Activity Status. What should be in the test method then when no input data is needed? I want the apex class to run once a day, should i create an apex class scheduler first and then create the test class that executes the class scheduler??

 

Chris

Ralph CallawayRalph Callaway

1. Update your class to provide a test hook that limits the results for your queries, this will allow you to control the results of your queries

 

public Set<Id> opptyIds { get; set; }
List<Opportunity> Opptys2;
if(opptyIds == null)
Opptys2 = [SELECT Opportunity_Number__c, LastActivityDate, Activity_Status__c FROM Opportunity ORDER BY LastActivityDate];
else
Opptys2 = [SELECT Opportunity_Number__c, LastActivityDate, Activity_Status__c FROM Opportunity ORDER BY LastActivityDate where id in :opptyIds];

 2. In your test class

a. Create opportunities and opportunity feeds that you'll iterate over in your test class

b. Pass the ids of your test data into the test class via the hooks created in step 1

c. Run the Opportunity method

d. Query your opportunities again and verify they've been updated as you would expect.

 

Regarding scheduling it to run everyday you can probably skip that.  Test methods are run every time somebody makes a deployment and will block changes that break the tests.

 

You should also read this article and do it's examples.

http://wiki.developerforce.com/index.php/How_to_Write_Good_Unit_Tests 

 

 

 

Dchris222Dchris222

I am running into problems setting up the test hook. It seems that I can't use the if statement after the List, it says expecting curly bracket, found if.

Ralph CallawayRalph Callaway

Keep at it.  I'm sure you'll get it.  Just be patient and play around with things.

Dchris222Dchris222

Is there a way to see if my apex class is even coded correctly without creating a test class? Because, if my apex class isn't coded correctly then the test class is useless....

Ralph CallawayRalph Callaway

Just test it manually.  Open up the system log and run your code.

 

OppPoliceController controller = new OppPoliceController();
controller.opportunity();

 If you want to debug throw in a bunch of print statements - system.debug('message') - to see what's going on.