-3

What does the below line of code mean?

String ProcessStr =(!test.isRunningTest())? Customsetting__c.getInstance('Process').value__c:'0';
  • 1
    Note: this is Bad Code. If you need the value of a custom setting in a unit test, the custom setting should be created as part of the test setup. – sfdcfox Feb 20 '18 at 16:16
  • Never use Test.isRunningTest unless you can find no alternative. – Adrian Larson Feb 20 '18 at 16:37
5

The documentation is quite clear on what this specific method does, and the name is essentially self documenting:

  • isRunningTest()
    Returns true if the currently executing code was called by code contained in a test method, false otherwise. Use this method if you need to run different code depending on whether it was being called from a test.

As for ternary syntax (?:), you can see that documented in the Apex Developer Guide under Expression Operators:

Operator
? :

Syntax
x ? y : z

Description
Ternary operator (Right associative). This operator acts as a short-hand for if-then-else statements. If x, a Boolean, is true, y is the result. Otherwise z is the result. Note that x cannot be null.

So the translation of your code to plain English is essentially:

If this code is running in a live transaction (not a test), then get the value from a custom setting. Otherwise, use a value of '0'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.