Skip to main content
Hi,

I'm working on the ExternalSearch_Tests Apex class and trying to get my negative scenario to pass, but it won't.  I know my logic isn't correct so may I get some help? (Technical pointers and not necessarily the answer.  I'll figure it out.  I'm not sure what I'm doing being still a beginner with Apex.  Thanks)

@isTest static void test_method_negative() {

    HttpMockFactory mock = new HttpMockFactory(500, 'Internal Server Error', 'Did not recieve a 200 status code: ', new Map<String,String>());

    Test.setMock(HttpCalloutMock.class, mock);

    String result;

    Test.startTest();

      result = ExternalSearch.googleIt('!');

    Test.stopTest();

    system.assertEquals('Did not recieve a 200 status code: ', result); 

  }

Reference trailhead "Unit Testing on the Lightning Platform (https://trailhead.salesforce.com/en/content/learn/modules/unit-testing-on-the-lightning-platform" style="color:⌗0563c1; text-decoration:underline)" - module "Use Mocks and Stub Objects".
3 answers
  1. Mar 21, 2024, 11:32 PM

    Try to put your code in a "TRY CATCH" sentense

     

    Test.startTest();

    try{

    result = ExternalSearch.googleIt('server issue');

    }catch(ExternalSearch.ExternalSearchException e){

    System.assertEquals('Did not receive a 200 status code: 500', e.getMessage(), 'Internal Server Error');

    }

    Test.stopTest();

0/9000