To implement the Callable interface, you need to write only one method: call(String action, Map<String, Object> args).
In code that utilizes or tests an implementation of Callable, cast an instance of your type to Callable.
This interface is not intended to replace defining more specific interfaces. Rather, the Callable interface allows integrations in which code from different classes or packages can use common base types.
The following are methods for Callable.
public class Extension implements Callable { // Actual method String concatStrings(String stringValue) { return stringValue + stringValue; } // Actual method Decimal multiplyNumbers(Decimal decimalValue) { return decimalValue * decimalValue; } // Dispatch actual methods public Object call(String action, Map<String, Object> args) { switch on action { when 'concatStrings' { return this.concatStrings((String)args.get('stringValue')); } when 'multiplyNumbers' { return this.multiplyNumbers((Decimal)args.get('decimalValue')); } when else { throw new ExtensionMalformedCallException('Method not implemented'); } } } public class ExtensionMalformedCallException extends Exception {} }
@IsTest private with sharing class ExtensionCaller { @IsTest private static void givenConfiguredExtensionWhenCalledThenValidResult() { // Given String extensionClass = 'Extension'; // Typically set via configuration Decimal decimalTestValue = 10; // When Callable extension = (Callable) Type.forName(extensionClass).newInstance(); Decimal result = (Decimal) extension.call('multiplyNumbers', new Map<String, Object> { 'decimalValue' => decimalTestValue }); // Then System.assertEquals(100, result); } }