function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
balaji raobalaji rao 

Hi all, I written a test class for Rest api controller.but in this test class i am not able to cover the access token and json string and endpoint please help me out ?

Hi all, I written a test class for Rest api controller.but in this test class i am not able to cover the access token and json string and endpoint please help me out ?
GovindarajGovindaraj
Hi Balaji,

Did you try the mock callout ?

Below is the sample code,
public class MockHttpResponseGenerator implements HttpCalloutMock {
    private Integer mockCount = 0;
    // Implement this interface method
    public HTTPResponse respond(HTTPRequest req) {
        // Create a fake response (first mock is access_token, second mock is client id)
        mockCount++;
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody(mockCount == 1
                    ? '{"access_token":"somevalidAccessToken"}'
                    : '{"client_id" : "someValidClientId"}'  
                   );
        res.setStatusCode(200);
        return res;
    }
}
Thanks,
Govindaraj.S
balaji raobalaji rao
Hi Govindaraj.S 
yah, I have used Mock class.Same as the above.
I need to External Id in test class .
Please help me
balaji raobalaji rao
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, COH_BP_FeedItemTrigger: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.COH_BP_FeedItemHelper.processNewFeedItems: line 354, column 1
Trigger.COH_BP_FeedItemTrigger: line 15, column 1: []
GovindarajGovindaraj
Hi Balaji,

Can you please post your trigger code here ? or else refer below test class. Here, 'Foreign_Key__c' is the external Id.
 
@isTest
private class TestSurveyTrigger {
    @isTest
    static void myUnitTest() {
        Account testAccount = new Account(Name='My test account', Foreign_Key__c='test123');
        insert testAccount; 
        Test.startTest();
        Survey__c testSurvey = new Survey__c(Account_Key__c='test123');
        insert testSurvey;
        Test.stopTest();
        Survey__c result = [select Id, Account__c, Account__r.Name, Account__r.Foreign_Key__c from Survey__c where Account_Key__c = 'test123' limit 1];
        system.assertEquals(testAccount.Id, result.Account__c);
    }
}

Thanks,
Govindaraj.S