+ Start a Discussion
Tien Le 488Tien Le 488 

need help getting externalsearch_tests negative scenario to PASS

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".
Tien Le 488Tien Le 488
I've tried and tried all kinds of experiments, but I'm not savvy enough in my beginner phase to solve my problem.  I'm spinning my wheels here.  Thanks in advance for your help.
Tien
 
lily wu 9lily wu 9
Not sure if you got solution already. here is what i have done, you can either remove the exception handle for non 200/500 statuscode. or you can add one more testing to cover the exception handling.  

solution 1:

ExternalSearch.cls 
...
 req.setEndpoint('https://www.google.com?q='+query);
    req.setMethod('GET');
    HttpResponse res = h.send(req);
    return res.getBody();
...
ExternalSearch_Tests.cls
 @isTest static void test_method_two() {
    HttpMockFactory mock = new HttpMockFactory(500, 'Internal Server Error', 'server issue!', new Map<String,String>());
    Test.setMock(HttpCalloutMock.class, mock);
    String result;
    Test.startTest();
      result = ExternalSearch.googleIt('server issue');
    Test.stopTest();
    system.assertEquals('server issue!', result); 
  }

Solution 2

ExternalSearch.cls 
...
 req.setEndpoint('https://www.google.com?q='+query);
    req.setMethod('GET');
    HttpResponse res = h.send(req);
    if(res.getStatusCode() != 200 && res.getStatusCode() != 500){       
         throw new ExternalSearchException('Did not recieve 200/500 status code' );                 
     }
    return res.getBody();
...

ExternalSearch_Tests.cls
 @isTest static void test_method_two() {
    HttpMockFactory mock = new HttpMockFactory(500, 'Internal Server Error', 'server issue!', new Map<String,String>());
    Test.setMock(HttpCalloutMock.class, mock);
    String result;
    Test.startTest();
      result = ExternalSearch.googleIt('server issue');
    Test.stopTest();
    system.assertEquals('server issue!', result); 
  }

  @isTest static void test_method_three() {
    HttpMockFactory mock = new HttpMockFactory(404, 'Server not found', 'server not found!', new Map<String,String>());
    Test.setMock(HttpCalloutMock.class, mock);
    String result;
    Test.startTest();
         try {
              result = ExternalSearch.googleIt('server not found');
         }
         catch (ExternalSearch.ExternalSearchException exc){
             system.assert(exc.getMessage().equalsIgnoreCase('Did not recieve 200/500 status code'));
         }
     
    Test.stopTest();    
  }