Skip to main content

Trailhead link : https://trailhead.salesforce.com/content/learn/modules/unit-testing-on-the-lightning-platform/mock-stub-objects?trailmix_creator_id=vdasari27&trailmix_slug=apex-testing-salesforce

 

test class written:

 

@IsTest

private class ExternalSearchTests {

    @IsTest

    static void testPositiveMocking() {

        // GIVEN

        HTTPMockFactory mock = new HTTPMockFactory(

            200,

            'OK',

            'I found it!',

            new Map<String, String>()

        );

        Test.setMock(HttpCalloutMock.class, mock);

        // WHEN

        Test.startTest();

        String result = ExternalSearch.googleIt('epic search');

        Test.stopTest();

        // THEN

        Assert.areEqual('I found it!', result, 'Expected to receive mock response');

    }

    @IsTest

    static void testNegativeMocking(){

       

        HTTPMockFactory hmock =new HTTPMockFactory(500,'Server Error', 'Not found', new Map<String, String>());

        Test.setMock(HttpCalloutMock.class, hmock);

        Test.startTest();

        String result = ExternalSearch.googleIt('Ramsay');

        Test.stopTest();

       

    }

}

 

Challenge is not getting completed, is there any problem with the test class written?Facing issue with 'Use Mock and Stub Objects' trailhead

2 answers
0/9000