• Home
  • About
    • Leadership
    • Partners
    • Blogroll
  • Force-Squared Blog
    • Tips and Tricks
    • Configuration
    • Development
  • Support
    • Knowledge Base
    • Submit a Case
  • Is It Dreamforce Yet?

X-Squared On Demand

Salesforce solutions delivered

  • Home
  • About
    • Leadership
    • Partners
    • Blogroll
  • Force-Squared Blog
    • Tips and Tricks
    • Configuration
    • Development
  • Support
    • Knowledge Base
    • Submit a Case
  • Is It Dreamforce Yet?
  • Tips and Tricks
  • Configuration
  • Development
You are here: Home / Salesforce CRM / Development / Apex / System Replacement For isTest Apex Method

System Replacement For isTest Apex Method

February 23, 2011 by David Schach 1 Comment

Sometimes we have to write code that executes differently if the Apex is being tested. For a great example, check out Scott Hemmeter's blog post on testing webservice callouts at http://sfdc.arrowpointe.com/2009/05/01/testing-http-callouts/.

Scott's example works well, and he uses a Boolean isApexTest, running certain code if this is true or false. I used to do something similar.

The disadvantage is that one has to declare one more variable, assign a new value to it from the test code (or call a special method from the test code which, in my opinion, negatively impacts code readability), and (if you put your test code in a separate class) declare it as public. My Java professor would not like this, as he preferred to declare all variables private unless absolutely necessary. Sure, I could make a private Boolean and a getter, but now we're splitting hairs.

Salesforce has come to the rescue, though, with a test.isRunningTest() method. Basically, you can use this interchangeably with your isApexTest variable.

Here's a snippet of code (from Scott's blog post) with the old and new methods:

public static boolean isApexTest = false;
public String main(){
    // Build the http request
    // Invoke web service call
    String result = '';
    if (!isApexTest){
        // Make a real callout since we are not running a test
    } else {
        // A test is running
        result = FakeXMLReturnToSimulateResponse;
    }
    return result;
}
And now with the new method:
public String main(){
    // Build the http request
    // Invoke web service call
    String result = '';
    if (!Test.isRunningTest()){
        // Make a real callout since we are not running a test
    } else {
        // A test is running
        result = FakeXMLReturnToSimulateResponse;
    }
    return result;
}
Some among you may want to switch the two so that we don't put a negative method response in the if evaluator, and that's okay too.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to email a link to a friend (Opens in new window) Email

Related

Filed Under: Apex, Development, Salesforce, Winter 11 Tagged With: Apex, New Features

← Salesforce Wallpaper for iPad (by request) Visualforce Inline Editing – I’m In Love →

Comments

  1. Venkat Polisetti says

    November 13, 2011 at 14:46

    David,

    Excellent. I have set up a salesforce site in 2008 for our webinar registrations and I had to do exactly what you showed above with a temp variable that tracks if you are testing your code or running in real time.

    Recently, I was making some changes to that old code and was thinking about this and I was sure, salesforce would have fixed this annoyance with a method call and exactly they did. My googling sent me to your site and glad you talked about it.

    Thanks,
    Venkat Polisetti

    Reply

Share Your ThoughtsCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Is it Dreamforce Yet?

Find out!

Find us on the AppExchange

Mass Update Contacts 2.0

Get It Now

Recent Posts

  • Prevent Duplicate Emails on Leads
  • Duplicate Record Item Enrichment and Auto-Deletion Code
  • Lightning Component With Running User Information
  • ChatterBINGO is Now Open-Source
  • Display Only My Role’s Records on a Report

Post Categories

Popular Tags

#df09 #df10 Akismet Apex AppBuilder AppExchange Appirio Astadia Blogs Chatter Configuration Content DreamFactory Dreamforce Eclipse IDE Enterprise Force.com Builder Force.com Platform Google Infowelders Integration Just for fun Lightning New Developments New Features Partner Program PersonAccount Projects Publisher Salesforce Salesforce1 Salesforce for Google Apps sfdcverse Sites Visualforce Web-to X-Squared On Demand

Find Us Online

  • Twitter
  • Facebook
  • LinkedIn
  • RSS

Subscribe

RSS Feed Comments

Subscribe via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 164 other subscribers

Copyright © 2008–2025 X-Squared On Demand · Genesis Framework by StudioPress · WordPress · Log in