Created
July 9, 2019 18:38
-
-
Save seanpat09/523cb152ba6cc790ff2436658af9e34e to your computer and use it in GitHub Desktop.
StubApiExample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************************************************* | |
public without sharing class Telemetry { | |
@TestVisible | |
private FeatureManagementWrapper featureManager = new FeatureManagementWrapper(); | |
public void process() { | |
featureManager.setPackageIntegerValue( | |
'NumberOfClosedOpportunities', | |
[SELECT Id FROM Opportunity WHERE IsClosed = true].size() | |
); | |
featureManager.setPackageBooleanValue( | |
'HasOrders', | |
[SELECT Id FROM Order LIMIT 1].size() > 0 | |
); | |
} | |
} | |
/******************************************************************************************************************* | |
public with sharing class FeatureManagementWrapper { | |
/** | |
* @description Wrapper for System.FeatureManagement.setPackageBooleanValue | |
*/ | |
public void setPackageBooleanValue(String apiName, Boolean value) { | |
System.FeatureManagement.setPackageBooleanValue(apiName, value); | |
} | |
/** | |
* @description Wrapper for System.FeatureManagement.setPackageIntegerValue | |
*/ | |
public void setPackageIntegerValue(String apiName, Integer value) { | |
System.FeatureManagement.setPackageIntegerValue(apiName, value); | |
} | |
} | |
//TESTS | |
/******************************************************************************************************************* | |
@IsTest | |
private class Telemetry_TEST { | |
@IsTest | |
private static void shouldCaptureTelemetry() { | |
final Integer NUM_OPPS = 10; | |
TestFixtures.insertClosedOpportunities(NUM_OPPS); | |
FeatureManagementWrapperMock mock = new FeatureManagementWrapperMock(); | |
Telemetry telemetryService = new Telemetry(); | |
telemetryService.featureManager = | |
(FeatureManagementWrapper) Test.createStub(FeatureManagementWrapper.class, featureManagementMock); | |
Test.startTest(); | |
telemetryService.process(); | |
Test.stopTest(); | |
System.assert(featureManagementMock.packageIntegerValuesByName.containsKey('NumberOfClosedOpportunities'), | |
'setPackageIntegerValue should be called with the feature "NumberOfClosedOpportunities"'); | |
System.assertEquals( | |
NUM_OPPS, | |
featureManagementMock.packageIntegerValuesByName.get('NumberOfClosedOpportunities'), | |
'The feature "NumberOfClosedOpportunities" should be set with the correct number of opps' | |
); | |
System.assert(featureManagementMock.setPackageBooleanValue.containsKey('HasOrders'), | |
'setPackageBooleanValue should be called with the feature "HasOrders"'); | |
System.assertEquals( | |
false, | |
featureManagementMock.setPackageBooleanValue.get('HasOrders'), | |
'The feature "HasOrders" should be set to false if there are no Orders' | |
); | |
} | |
private class FeatureManagementWrapperMock implements System.StubProvider { | |
private Map<String, Boolean> packageBooleanValuesByName = new Map<String, Boolean>(); | |
private Map<String, Integer> packageIntegerValuesByName = new Map<String, Integer>(); | |
private Object handleMethodCall( | |
Object stubbedObject, | |
String stubbedMethodName, | |
Type returnType, | |
List<Type> listOfParamTypes, | |
List<String> listOfParamNames, | |
List<Object> listOfArgs | |
) { | |
switch on stubbedMethodName { | |
when 'setPackageBooleanValue' { | |
packageBooleanValuesByName.put((String) listOfArgs[0], (Boolean) listOfArgs[1]); | |
} | |
when 'setPackageIntegerValue' { | |
packageIntegerValuesByName.put((String) listOfArgs[0], (Integer) listOfArgs[1]); | |
} | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment