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);
}
Best Answer
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 isolationyou have to create tour
Token__c
record before invokingCallout.accessTokenBody();
in test.