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
JeffTrierJeffTrier 

How do I make a Unit Test case for a Case Extension?

Hello everyone,

I am familiar with Unit Testing in general, but have been having a heck of a time implementing this using APEX with VisualForce Extensions. I have been searching the internet and docs for doing this, but I can't find a straightforward example. Can someone please post a unit test example for me? I would greatly appreciate it! 

Below is the class I am creating a Unit Test for. The VF page calling this extension is named "DynamicCase", and I am using the Force.com Eclipse plugin for this... if any of that helps. Thanks all!

-Jeff


public with sharing class VistaCaseExtension {

    // Create extension hook
    public ApexPages.StandardController controller {get;set;}
    
    // accessable variables
    public Case caseCont {get;set;}      // case controller global    
    public String testRet {get;set;}     // test return
    public String caseType {get;set;}    // used for caseType dropdown list

    // List objects for iteration    
    public List<Contact> contacts {get; set;}
    public List<Account> accounts {get; set;}
    
    // Constructor
    public VistaCaseExtension(ApexPages.StandardController std){
        // set global case controller
        controller = std;
        caseCont = (Case) controller.getRecord();
        
        // set default caseType selection state
        caseType = 'select';
        
        // populate account
        PopulateCase();
        
        
        testRet = 'SELECT CASE TYPE';
    }
    
    // populate the case page with all associated info
    public void PopulateCase() {
           
        // Retrieve Account for this Case
        if(caseCont.AccountId != NULL){
        
            // get all account info, to prepopulate fields
            getAccount(caseCont.AccountId);
            
            // get all contacts for this cases account, to prepopulate fields
            getAllContacts();
            
            // Set Primary Contact if field is empty
            if(caseCont.ContactID == NULL){
                setPrimaryContract();
            }                        
        }
                          
        // set ownerID if the field is empty
        if(caseCont.OwnerID == NULL){
            caseCont.OwnerID = UserInfo.getUserId();
        }
                
        // test
        testRet = caseCont.Type;
                            
    }  
    
    
    // Set Contact
    public void setPrimaryContract(){
        // Select the first contact from the account's list if none has been assigned
        //   or else assign the accounts primary technician
        if(caseCont.Account.PrimaryTechnicalContact__c == NULL){// use null, not ''
            //getPrimaryContact();
            // set the specified case accounts primary contact
            caseCont.Contact = [select id, phone, email, name
                                from Contact
                                where AccountID=:caseCont.AccountId
                                LIMIT 1];
                             
            // assign the contact ID                         
            caseCont.ContactID = caseCont.Contact.id;                    
        }else{
            caseCont.ContactID = caseCont.Account.PrimaryTechnicalContact__c;
        }    
    }
    
    // get list of all contacts associated with the cases account
    // used for contact lists
    public void getAllContacts(){
        contacts = [select id, Email, Phone, Name, Title
                    from Contact
                    where AccountID=:caseCont.AccountId
                    order by firstname asc];   
        
    }
    
    // populate account
    public void getAccount(String relObjectID){
        caseCont.Account = [select id, AccountNumber, ShippingStreet, Name, OwnerID, Phone, PrimaryTechnicalContact__c, Account_No__c
                            from Account
                            where id=:relObjectID
                            LIMIT 1];
    }

}

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

The basic process is as follows:

 

1) Create a record to test on.

2) Create a standard controller to test with.

3) Create your extension.

4) Test it.

 

I'm not going to try and parse your code right now, but here's a brief example:

 

@isTest
private class testcaseext {
  private static void testmethod test() {
    // create dummy contact, account, etc, if need be.
    case c = new case( /* set your fields here */ );
    // insert c; do this if your are testing edit mode, otherwise leave blank.
    apexpages.standardcontroller controller = new apexpages.standardcontroller(c);
    test.setcurrentpage(page.vistacasepage); // set this to the correct page!
    // set current page parameters here, if expected.
    test.starttest();
    vistacaseextension ext = new vistacaseextension(c);
    // call methods in your class here for testing.
    test.stoptest();
    // call system.assert or system.assertequals on data to ensure it worked!
  }
}

As an aside, since you have code branches, try to test each branch in a separate test method, setting up the expected inputs for each one before calling test.starttest() or constructing your extension class.

All Answers

sfdcfoxsfdcfox

The basic process is as follows:

 

1) Create a record to test on.

2) Create a standard controller to test with.

3) Create your extension.

4) Test it.

 

I'm not going to try and parse your code right now, but here's a brief example:

 

@isTest
private class testcaseext {
  private static void testmethod test() {
    // create dummy contact, account, etc, if need be.
    case c = new case( /* set your fields here */ );
    // insert c; do this if your are testing edit mode, otherwise leave blank.
    apexpages.standardcontroller controller = new apexpages.standardcontroller(c);
    test.setcurrentpage(page.vistacasepage); // set this to the correct page!
    // set current page parameters here, if expected.
    test.starttest();
    vistacaseextension ext = new vistacaseextension(c);
    // call methods in your class here for testing.
    test.stoptest();
    // call system.assert or system.assertequals on data to ensure it worked!
  }
}

As an aside, since you have code branches, try to test each branch in a separate test method, setting up the expected inputs for each one before calling test.starttest() or constructing your extension class.

This was selected as the best answer
JeffTrierJeffTrier

Thanks sfdcfox,

 

That code sample should get me on my way!

 

-Jeff