2

I wrote the below test classes -

@isTest
global class ProjectCalloutServiceMock implements HttpCalloutMock{
    global HttpResponse respond(HttpRequest req){
        System.Debug('--++-- Inside success response'); 
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type','application/json');
        response.setBody('{"status":"success"}');
        response.setStatusCode(201);
        return response;
    }
}

@isTest
global class ProjectCalloutServiceMockFailure implements HttpCalloutMock {
   global HttpResponse respond(HttpRequest req){
        System.Debug('--++-- Inside failure response');       
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type','application/json');
        response.setBody('{"status":"failure"}');
        response.setStatusCode(500);
        return response;
    } 
}

@isTest
private class ProjectCalloutServiceTest {
    @isTest
    private static void testSuccessCallout(){
        ServiceTokens__c serviceToken = new ServiceTokens__c(
                                            Name='ProjectServiceToken',
                                            Token__c='DUMMY-TOKEN'
                                        );
        insert serviceToken;

        Account a = new Account(Name = 'Acme');
        insert a;
        Opportunity o = new Opportunity(
                            Name='Test',
                            StageName='Submitted Project',
                            AccountId=a.Id,
                            Amount=1000,
                            CloseDate=Date.Today()
                        );
        insert o;
        List<Id> listOppsIds = new List<Id>();
        listOppsIds.add(o.Id);

        Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock());
        Test.startTest();
        ProjectCalloutService.postOpportunityToPMS(listOppsIds);
        Test.stopTest();

        Opportunity fetchedOpp = [SELECT StageName From Opportunity WHERE ID =: o.Id];
        System.assertEquals('Submitted Project', fetchedOpp.StageName);
    }

    @isTest
    private static void testFailureCallout(){
        ServiceTokens__c serviceToken = new ServiceTokens__c(
                                            Name='ProjectServiceToken',
                                            Token__c='DUMMY-TOKEN'
                                        );
        insert serviceToken;

        Account a = new Account(Name = 'Acme');
        insert a;
        Opportunity o = new Opportunity(
                            Name='Test',
                            StageName='Submitted Project',
                            AccountId=a.Id,
                            Amount=1000,
                            CloseDate=Date.Today()
                        );
        insert o;
        List<Id> listOppsIds = new List<Id>();
        listOppsIds.add(o.Id);

        Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMockFailure());
        Test.startTest();
        ProjectCalloutService.postOpportunityToPMS(listOppsIds);
        Test.stopTest();

        Opportunity fetchedOpp = [SELECT StageName From Opportunity WHERE ID =: o.Id];
        System.assertEquals('Resubmit Project', fetchedOpp.StageName);
    }

}

The test method testFailureCallout is failing with the below message(assert failure) -

System.AssertException: Assertion Failed: Expected: Resubmit Project, Actual: Submitted Project

Below is the code snippet of my callout method -

HttpResponse response = h.send(req);
System.Debug('--++-- Response: ' + response);

if(response.getStatusCode() == 201){
    System.Debug('--++-- SUCCESS');
    singleOpportunity.StageName = 'Submitted Project';
}else if(response.getStatusCode() == 500){
    System.Debug('--++-- FAILURE');
    singleOpportunity.StageName = 'Resubmit Project';
}

Database.update(singleOpportunity);

When I enabled the debug log, I noticed that instead of failure mock, it is returning what is there in the success mock service. Not able to find out the reason though. Any help will be highly appreciated. Thanks.

  • Well yea, in your insert you're inserting an oppty and then using that ID to query out the same oppty with that Stagename – EricSSH Feb 11 '17 at 8:54
  • @EricSSH - In the last bit of code the OP shows that for a failed callout the StageName is updated. So from what I can tell in the code is should pass as the 500 response block should be entered..unless I am missing something ....We would need debug logs to see what is going on.... – Eric Feb 12 '17 at 4:50
  • When I checked the debug log, it is not going to the failed mocked service at all. But as per the test mock, it should go. Based on the response code, I am updating the Opportunity StageName. – Sudipta Deb Feb 12 '17 at 15:38
  • You said "it is returning what is there in the success mock service" is it doing that or not? Maybe you have an error that you are swallowing in an empty try/catch block? Without your code or debug logs it is impossible to say as the code as you posted it of and by itself looks like it should work fine – Eric Feb 13 '17 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.