4

I am using an app exchange package in my code, they have exposed a few global methods that I can use to get my job done. It all works beautifully. The issue comes with testing.

I want to test positive, negative test scenarios to see my code handle's App Exchange packages' response elegantly.

That app exchange package does callout and parses, I don't know the actual HTTP Response JSON structure or mock so I cannot use Test.setMock.

The next thing I investigated is to use Stub API. Which sounded perfect but when I tried this mock I got an exception.

System.TypeException: Test.createStub() can only be called with classes in the current namespace

AppExchange Package Code:

AppExchangePackage.Api api = new AppExchangePackage.Api();
AppExchangePackage.ApiData.ScreenResponse response = api.screen(entityId); //Can be PersonAccount , Lead, Contact Id

Is there an elegant way to mock api.screen method so that I get my desired ScreenResponse in test context?

3

I cannot mock app-exchange package method, but I can mock my method, which calls the package. This will stop the code entering package and get me to do what I want.

public class APIProcessingService
{

    public ppExchangePackage.ApiData.ScreenResponse submit(Id entityId){
        AppExchangePackage.Api api = new AppExchangePackage.Api();
        AppExchangePackage.ApiData.ScreenResponse response = api.screen(entityId); //Can be PersonAccount , Lead, Contact Id
        return response;
    }

} 

Test:

@isTest
public class APIProcessingServiceTest{


    public class APIProcessingMockProvider implements System.StubProvider {

        public Object handleMethodCall(Object stubbedObject, String stubbedMethodName,
                Type returnType, List<Type> listOfParamTypes, List<String> listOfParamNames,
                List<Object> listOfArgs) {


            if(stubbedMethodName =='submit'){
                return new AppExchangePackage.ApiData.ScreenResponse(); //My Mock for success or failure response

            }

            return null;
        }
    }

    @isTest
    public static void testMethodPositive(){

        Account acc =  [SELECT  Id FROM  Account LIMIT  1];

        APIProcessingService rs = (APIProcessingService) Test.createStub(APIProcessingService.class , new APIProcessingMockProvider());
        Object resp =rs.submit(acc.Id);
        System.debug(resp);



    }
}

The only downside is, as the code is not entering the package, the package level validations won't work.

  • 2
    +1 (so rare to see a use of System.StubProvider) – cropredy Jul 22 '19 at 17:20
  • @cropredy Yeah, System.StubProvider is quite powerful tbh, It gives me real feeling that I am mocking. ApexMocks , fflib have good mocking, but a steep learning curve – Pranay Jaiswal Jul 23 '19 at 20:09
  • 1
    you should reach out to me on chat some time -- I can help with apexmocks – cropredy Jul 23 '19 at 20:11

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.