LogicLuminaryy / Solution-Apex-Callouts-Superbadge-Unit-

Apex Callouts Superbadge Unit
4 stars 0 forks source link

Challenge 2 is getting failed #4

Open MS167978 opened 6 days ago

MS167978 commented 6 days ago

I have added all the provided Apex classes and executed the test class. In my org, the current code coverage is 86%. However, I am unable to reach 90% coverage because the test class does not cover the following code in the Apex class.

Apex Copy code:-

} catch (Exception e) {
    System.debug('Error: ' + e.getMessage());
    statusCode = 500; // Internal Server Error
}

Could you please help me resolve this issue?"

LogicLuminaryy commented 6 days ago

What are lines it's not covering?

On Mon, Sep 30, 2024 at 1:55 PM maheshsaboli @.***> wrote:

I have added all the provided Apex classes and executed the test class. In my org, the current code coverage is 86%. However, I am unable to reach 90% coverage because the test class does not cover the following code in the Apex class.

Apex Copy code:-

} catch (Exception e) { System.debug('Error: ' + e.getMessage()); statusCode = 500; // Internal Server Error }

Could you please help me resolve this issue?"

— Reply to this email directly, view it on GitHub https://github.com/LogicLuminaryy/Solution-Apex-Callouts-Superbadge-Unit-/issues/4, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHBACMEGM42TRU3PRT6B27TZZEDIFAVCNFSM6AAAAABPCXP3ROVHI2DSMVQWIX3LMV43ASLTON2WKOZSGU2TKOJSHEYDQMQ . You are receiving this because you are subscribed to this thread.Message ID: <LogicLuminaryy/Solution-Apex-Callouts-Superbadge-Unit-/issues/4@ github.com>

MS167978 commented 6 days ago

Line numbers 17 and 19 are highlighted below:-

Screenshot 2024-09-30 at 3 16 55 PM
LogicLuminaryy commented 6 days ago

send me your both class and test class please

On Mon, Sep 30, 2024 at 3:16 PM MS @.***> wrote:

Line number 17 and 19 which are highlighted below:-

} catch (Exception e) { System.debug('Error: ' + e.getMessage()); statusCode = 500; // Internal Server Error }

— Reply to this email directly, view it on GitHub https://github.com/LogicLuminaryy/Solution-Apex-Callouts-Superbadge-Unit-/issues/4#issuecomment-2382643298, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHBACMB77ICSEXAGYGBFMWDZZEMYJAVCNFSM6AAAAABPCXP3ROVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOBSGY2DGMRZHA . You are receiving this because you commented.Message ID: <LogicLuminaryy/Solution-Apex-Callouts-Superbadge-Unit-/issues/4/2382643298 @github.com>

MS167978 commented 6 days ago

Apex Class:- 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
    }
    return statusCode;
}

}


Test Class:- @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');
}

}


Mock Test class:- @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; } }

LogicLuminaryy commented 6 days ago

Let me see

On Mon, 30 Sep 2024 at 3:21 PM, MS @.***> wrote:

Apex Class:- 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
}
return statusCode;

}

}

Test Class:- @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');

}

}

Mock Test class:- @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; } }

— Reply to this email directly, view it on GitHub https://github.com/LogicLuminaryy/Solution-Apex-Callouts-Superbadge-Unit-/issues/4#issuecomment-2382653771, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHBACMCUXIXWWOHKWTGEGALZZENLRAVCNFSM6AAAAABPCXP3ROVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOBSGY2TGNZXGE . You are receiving this because you commented.Message ID: <LogicLuminaryy/Solution-Apex-Callouts-Superbadge-Unit-/issues/4/2382653771 @github.com>

MS167978 commented 6 days ago

Sure, Thank you.

LogicLuminaryy commented 5 days ago

`@IsTest public class RewardsCalloutServiceTest {

@IsTest
static void testSubmitUsersForRewardCalloutSuccess() {
    // Setup mock response for the HTTP callout
    Test.setMock(HttpCalloutMock.class, new RewardsCalloutMockSuccess());

    // Example JSON body to send
    String jsonBody = '{"userId": "001XX000003DHPc"}';

    // Call the method
    Integer statusCode = RewardsCalloutService.submitUsersForRewardCallout(jsonBody);

    // Verify the status code is as expected (mocked 200)
    System.assertEquals(200, statusCode, 'The status code should be 200 for a successful callout');
}

@IsTest
static void testSubmitUsersForRewardCalloutFailure() {
    // Setup mock response for the HTTP callout to simulate failure
    Test.setMock(HttpCalloutMock.class, new RewardsCalloutMockFailure());

    // Example JSON body to send
    String jsonBody = '{"userId": "001XX000003DHPc"}';

    // Call the method
    Integer statusCode = RewardsCalloutService.submitUsersForRewardCallout(jsonBody);

    // Verify the status code is as expected (mocked failure 500)
    System.assertEquals(500, statusCode, 'The status code should be 500 for a failed callout');
}

// Mock class for a successful callout
private class RewardsCalloutMockSuccess implements HttpCalloutMock {
    public HTTPResponse respond(HTTPRequest req) {
        HttpResponse res = new HttpResponse();
        res.setStatusCode(200);
        res.setBody('{"message": "Success"}');
        return res;
    }
}

// Mock class for a failed callout
private class RewardsCalloutMockFailure implements HttpCalloutMock {
    public HTTPResponse respond(HTTPRequest req) {
        HttpResponse res = new HttpResponse();
        res.setStatusCode(500);
        res.setBody('{"message": "Internal Server Error"}');
        return res;
    }
}

} `

danielFuentes01 commented 5 days ago

with this code the code coverage of the WellnessJourneyRewardsBatch turns to 0%