Open mginocchioSF opened 1 month ago
Hi @mginocchioSF,
Use the below code provided.
* RewardsCalloutServiceMock**
@isTest
global class RewardsCalloutServiceMock implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"status":"success"}');
res.setStatusCode(200);
return res;
}
}
* RewardsCalloutServiceTest**
@isTest
public class RewardsCalloutServiceTest {
@isTest static void testSubmitUsersForRewardCallout() {
Test.setMock(HttpCalloutMock.class, new RewardsCalloutServiceMock());
// Create test data for Wellness Journey
List<Wellness_Journey__c> journeys = new List<Wellness_Journey__c>();
User testUser = [SELECT Id, Name, Username, Email FROM User WHERE Id = :UserInfo.getUserId()];
for (Integer i = 0; i < 12; i++) {
Wellness_Journey__c journey = new Wellness_Journey__c(
OwnerId = testUser.Id
);
journeys.add(journey);
}
insert journeys;
Test.startTest();
Database.executeBatch(new WellnessJourneyRewardsBatch());
Test.stopTest();
// Assertions to validate the behavior can be added here
Integer statusCode = RewardsCalloutService.submitUsersForRewardCallout(JSON.serialize(journeys));
System.assertEquals(200, statusCode, 'The HTTP status code should be 200');
}
@isTest static void testException() {
Test.setMock(HttpCalloutMock.class, new RewardsCalloutServiceErrorMock());
Test.startTest();
Database.executeBatch(new WellnessJourneyRewardsBatch());
Test.stopTest();
String journeys = 'Test';
// Assertions to validate the behavior can be added here
Integer statusCode = RewardsCalloutService.submitUsersForRewardCallout(JSON.serialize(journeys));
System.debug(statusCode);
System.assertEquals(500, statusCode, 'The HTTP status code should be 200');
}
}
** RewardsCalloutService**
public class RewardsCalloutService {
public static Integer submitUsersForRewardCallout(String jsonBody) {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:BalancedLiving/rewards');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setBody(jsonBody);
Http http = new Http();
HttpResponse res;
Integer statusCode;
/*try {
res = http.send(req);
statusCode = res.getStatusCode();
System.debug('Response Status: ' + statusCode);
System.debug('Response Body: ' + res.getBody());
} catch (Exception e) {
System.debug('Error: ' + e.getMessage());
statusCode = 500; // Internal Server Error
}*/
res = http.send(req);
statusCode = res.getStatusCode();
System.debug('Response Status: ' + statusCode);
System.debug('Response Body: ' + res.getBody());
return statusCode;
}
}
please let me know if it helped to resolve your issue.
Hello, I tested the new code and it works.
Thank you for your support
Best regards
Monica
On Sat, Oct 19, 2024 at 5:03 PM MS @.***> wrote:
Hi @mginocchioSF https://github.com/mginocchioSF,
Use the below code provided.
** RewardsCalloutServiceMock* @istest https://github.com/istest global class RewardsCalloutServiceMock implements HttpCalloutMock { global HTTPResponse respond(HTTPRequest req) { HttpResponse res = new HttpResponse(); res.setHeader('Content-Type', 'application/json'); res.setBody('{"status":"success"}'); res.setStatusCode(200); return res; } }
** RewardsCalloutServiceTest* @istest https://github.com/istest public class RewardsCalloutServiceTest { @istest https://github.com/istest static void testSubmitUsersForRewardCallout() { Test.setMock(HttpCalloutMock.class, new RewardsCalloutServiceMock());
// Create test data for Wellness Journey List<Wellness_Journey__c> journeys = new List<Wellness_Journey__c>(); User testUser = [SELECT Id, Name, Username, Email FROM User WHERE Id = :UserInfo.getUserId()]; for (Integer i = 0; i < 12; i++) { Wellness_Journey__c journey = new Wellness_Journey__c( OwnerId = testUser.Id ); journeys.add(journey); } insert journeys; Test.startTest(); Database.executeBatch(new WellnessJourneyRewardsBatch()); Test.stopTest(); // Assertions to validate the behavior can be added here Integer statusCode = RewardsCalloutService.submitUsersForRewardCallout(JSON.serialize(journeys)); System.assertEquals(200, statusCode, 'The HTTP status code should be 200');
}
@isTest static void testException() { Test.setMock(HttpCalloutMock.class, new RewardsCalloutServiceErrorMock());
Test.startTest(); Database.executeBatch(new WellnessJourneyRewardsBatch()); Test.stopTest();
String journeys = 'Test'; // Assertions to validate the behavior can be added here Integer statusCode = RewardsCalloutService.submitUsersForRewardCallout(JSON.serialize(journeys)); System.debug(statusCode); System.assertEquals(500, statusCode, 'The HTTP status code should be 200'); }
}
*** RewardsCalloutService* public class RewardsCalloutService { public static Integer submitUsersForRewardCallout(String jsonBody) { HttpRequest req = new HttpRequest(); req.setEndpoint('callout:BalancedLiving/rewards'); req.setMethod('POST'); req.setHeader('Content-Type', 'application/json'); req.setBody(jsonBody);
Http http = new Http(); HttpResponse res; Integer statusCode; /*try { res = http.send(req); statusCode = res.getStatusCode(); System.debug('Response Status: ' + statusCode); System.debug('Response Body: ' + res.getBody()); } catch (Exception e) { System.debug('Error: ' + e.getMessage()); statusCode = 500; // Internal Server Error }*/ res = http.send(req); statusCode = res.getStatusCode(); System.debug('Response Status: ' + statusCode); System.debug('Response Body: ' + res.getBody()); return statusCode;
}
}
please let me know if it helped to resolve your issue.
— Reply to this email directly, view it on GitHub https://github.com/LogicLuminaryy/Solution-Apex-Callouts-Superbadge-Unit-/issues/6#issuecomment-2423938857, or unsubscribe https://github.com/notifications/unsubscribe-auth/BMDF6WQP4XNBEFTVO2XAIRTZ4JYFFAVCNFSM6AAAAABP5IE5HKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIMRTHEZTQOBVG4 . You are receiving this because you were mentioned.Message ID: <LogicLuminaryy/Solution-Apex-Callouts-Superbadge-Unit-/issues/6/2423938857 @github.com>
--
Grazie/Thank you
Monica Ginocchio Principal Account SE *| *Salesforce
mobile: +39 347 8310317
email: *mginocchio@ @.>salesforce.com @.>* Galleria Passarella,2 Milan, Italy
I try to run a lot of time the RUN ALL TEST but I obtain the 84% code coverage.... I verify and the code is correct. Could you please help me?