• Welcome
  • About
    • Leadership
    • Blogroll
  • Force-Squared Blog
  • Support
    • Knowledge Base
    • Log a Case
  • Is It Dreamforce Yet?
  • Welcome
  • About
    • Leadership
    • Blogroll
  • Force-Squared Blog
  • Support
    • Knowledge Base
    • Log 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

Java
1
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

Java
1
test.isRunningTest()

method. Basically, you can use this interchangeably with your

Java
1
isApexTest

variable.

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

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
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:
Java
1
2
3
4
5
6
7
8
9
10
11
12
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.

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 →

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

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

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Like Us on Facebook

Like Us on Facebook

Share This Page

Find Us Online

  • Twitter
  • Facebook
  • LinkedIn
  • Google Plus
  • X-Squared G+ Page
  • 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 5,089 other subscribers

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