0

I have an access token for my HTTP callouts. How can I test it if I wanna use Static Resource? But I can also use the HttpCalloutMock Interface. it doesn't matter

My access token is in custom object. I just call fields in the method.

public static String accessToken(){
//Error is here:
    Token__c tok = [SELECT ConsumerKey__c, ClientSecret__c, Username__c, Password__c, SecurityToken__c
                            FROM Token__c 
                            WHERE Name = 'OurToken'];  
    String consumerKey = tok.ConsumerKey__c;
    String consumerSecret = tok.ClientSecret__c;
    String username = tok.Username__c;
    String password = tok.Password__c + tok.SecurityToken__c;
    String request = 'grant_type=password&client_id=' + consumerKey +'&client_secret=' + consumerSecret +
                     '&username=' + username + '&password='+password;
    return request;
}

I have my test, but it's write me an Error:

System.QueryException: List has no rows for assignment to SObject

@isTest
static void testCallout() {
    Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());

    Http ourHttp = new Http();
    String requestBody = Callout.accessToken();  //error is here
    HttpRequest ourRequest = new HttpRequest();
    ourRequest.setBody(requestBody);
    ourRequest.setMethod('POST');
    ourRequest.setEndpoint(System.Label.URL + '/services/oauth2/token');
    HttpResponse response2 = ourHttp.send(ourRequest);
}
  • 1
    provide code, that throws exception. You didn't show us Callout.accessTokenBody method – Oleksandr Berehovskyi Oct 12 '18 at 11:43
  • Sorry. It's a accessToken() method. – MichaelLev19 Oct 12 '18 at 11:59
  • can you highlight a line in accessToken, where exception is thrown ? – Oleksandr Berehovskyi Oct 12 '18 at 12:02
  • Done. To bad that I can't answer here in russian :) – MichaelLev19 Oct 12 '18 at 12:37
2

in tests methods without seeAllData = true property of annotation @isTest only data created in test method and in methods annotated with @testSetup is visible. this is a copcept of test data isolation

you have to create tour Token__c record before invoking Callout.accessTokenBody(); in test.

@isTest
static void testCallout() {
    Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
    insert new Token__c(
        Name = 'OurToken',
        .....
        );
    Http ourHttp = new Http();
    String requestBody = Callout.accessToken();  //error is here
    HttpRequest ourRequest = new HttpRequest();
    ourRequest.setBody(requestBody);
    ourRequest.setMethod('POST');
    ourRequest.setEndpoint(System.Label.URL + '/services/oauth2/token');
    HttpResponse response2 = ourHttp.send(ourRequest);
}
  • Coverage is still null. It's our web token to make integration between 2 orgs. I don't think I can make it custom. Now I have: insert new Token__c(Name = 'OurToken', ConsumerKey__c = '3MVG9I1kFE5Iul2DdHpJB2OydGLbG***************************************, ClientSecret__c = '473837323*********', Username__c = 'email@email.com', SecurityToken__c = '4qVSDcdY****************', Password__c = 'mypassword'); – MichaelLev19 Oct 12 '18 at 11:41
  • Error is the same: System.QueryException: List has no rows for assignment to SObject – MichaelLev19 Oct 12 '18 at 11:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.