You need to sign in to do that
Don't have an account?
Tien 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".
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
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();
}