1

How can I test this method? I try the test code from trailhead but can't make any coverage.

Callout:

public class Token{
    public String accessToken{get;set;}    
}

public static HttpResponse postCallout(String TypeID) {
    //test record:
    Type__c typ = [SELECT Id, Name FROM Type__c WHERE Id =: TypeID];
    Http ourHttp = new Http();
    //Here are my consumer key, consumer secret, username, password and request:
    String requestBody = getAccess();  
    HttpRequest req = new HttpRequest();
    req.setBody(requestBody);
    req.setMethod('POST');
    req.setEndpoint('https://p21.lightning.force.com/services/oauth2/token'); 
    HttpResponse response = ourHttp.send(req);      
    Token authentication = (Token)JSON.deserialize(response.getbody(), Token.class);

    if(objAuthenticationInfo.accessToken != null){
        HttpRequest req2 = new HttpRequest();
        req2.setHeader('Authorization','Bearer ' + authentication.accessToken);
        req2.setHeader('Content-Type','application/json');
        req2.setHeader('accept','application/json');
        req2.setMethod('POST');

 req2.setEndpoint('https://p21.lightning.force.com/services/apexrest/types/');
        req2.setBody(GenerateJSON(typ));
        response = ourHttp.send(req2);
        if(response .getStatusCode() == 200) {
                System.debug(response .getBody());
                return response ;
        }else {
            return null;
        }
      }
      return null;
    }
}

Mock:

 @isTest
global class mock implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest request) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"Name":"type"}');
        response.setStatusCode(200);
        return response; 
    }
}

Test:

@isTest static void testPostCallout() {
  Test.setMock(HttpCalloutMock.class, new mock()); 
  HttpResponse response = Classs.postCallout('a0A1r00005vIrVVN23');
  String contentType = response.getHeader('Content-Type');
  System.assert(contentType == 'application/json');
  String actualValue = response.getBody();
  System.debug(response.getBody());
  String expectedValue = '{"Name": "type"}';
  System.assertEquals(actualValue, expectedValue);
  System.assertEquals(200, response.getStatusCode());
}

Error:

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

2
  • You haven't showed us the actual mock you are using. We need to see the AnimalsHttpCalloutMock class.
    – Adrian Larson
    Oct 11, 2018 at 23:05
  • Sorry. Updated.
    – user60684
    Oct 12, 2018 at 4:18

1 Answer 1

3

The following error occurs when there are no records returned from a SOQL query and you try and assign the result to a single sObject:

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

Your test class needs to create at least one Type__c record before calling Classs.postCallout.

Be default tests run isolated from the data in an org, so they need to create anything they depend on.

E.g.

@isTest static void testPostCallout() {
    Test.setMock(HttpCalloutMock.class, new AnimalsHttpCalloutMock()); 
    Type__c testData = new Type__c();
    testData.Name = 'Foo';
    // TODO: populate any other required fields.
    insert testData;
    HttpResponse response = Classs.postCallout(testData.Id);
    //...
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .