/apex-httpmock

Everything the Salesforce developer needs to mock, stub and fake in Apex tests.

Primary LanguageApexMIT LicenseMIT

Apex HTTP Mock Codacy Badge

Simple library to make HTTP Mocks in Salesforce more readable by

  • using a fluent API
  • removing some of the boilerplate
Deploy to Salesforce
@IsTest
private class MyApi_Test {

    private static final PersonApi API = new PersonApi();
    private static final Person JOE = new Person('joe');
    private static final Person TIM = new Person('tim');


    @IsTest
    private static void multipleHttpMethods() {

        // Setup
        new HttpMock()
                // different methods
                .get('/v2/person/tim', TIM, 200)
                .post('/v2/person/tim', true, 200)

                // body conditions
                .post('/v2/person/tim#"age":33', true, 200)
                .post('/v2/person/tim#"age":null', false, 200)

                // failures
                .get('/v1/persons', false, 505)
                .get('/v2/persons', new CalloutException())
                
                .mock();


        // Exercise + Verify
        Test.startTest();

        System.assertEquals(true,  API.updatePerson(new Person('tim')) );
        System.assertEquals('tim', API.getPerson('tim').name);

        Test.stopTest();
    }
}