Skip to main content
Hi All, Unable to complete the test class as im getting the error "Rest API FATAL_ERROR System.QueryException: List has no rows for assignment to SObject In Test Class"

My Apex Class:

@RestResource(urlMapping='/GetContactId/*')

global without sharing class PersonContactRestEndpoint{

   

   @HttpGet

    global static string getPersonContactId() {

        RestRequest request = RestContext.request;

        

        // grab the EmailId & Brand from the end of the URL       

        String EmailId= RestContext.request.params.get('emailId');

        String Brand= RestContext.request.params.get('brand');

         

        system.debug('EmailId==>'+emailId);      

        system.debug('Brand==>'+brand);

        

        Account CustomerId=  [SELECT PersonContactId FROM Account WHERE CC_Brand__c = :Brand AND PersonEmail = :EmailId LIMIT 1];

        String PersonContactId = CustomerId.PersonContactId;

        

        system.debug('PersonContactId==>'+PersonContactId);

        return PersonContactId;          

    }

}

Test Class:

@IsTest

private class PersonContactRestEndpointTest

{

    static testMethod void testGetMethod()

    {

        Account acc = new Account();

        acc.Name='Test';

        acc.AccountNumber ='12345';

        insert acc;

        

      // Create Required data here  

    

        Test.startTest();

        RestResponse res = new RestResponse();

        RestRequest req = new RestRequest(); 

        

        req.params.put('PersonEmail', 'sschimpf@urbn.com');

        req.params.put('CC_Brand__c', 'Anthropologie');  // ==> CC_Brand__c is formula field

        req.params.put('PersonContactId', '0032h00000GfE6rAAF');

        

         req.httpMethod = 'Get';

         req.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated 

        // req.requestURI= URL.getOrgDomainUrl() +'/services/apexrest/GetContactId?brand=Anthropologie&emailId=sschimpf@urbn.com';

         //req.requestURI= URL.getOrgDomainUrl() +'/services/apexrest/GetContactId';

         req.requestURI= '/services/apexrest/GetContactId';

         RestContext.request = req;

         RestContext.response = res;

         string results = PersonContactRestEndpoint.getPersonContactId(); 

        Test.stopTest();

        

    }

}

Could you some one help me on this? Thanks in advance for the help..

Thanks..!!!

 
1 个回答
  1. 2021年2月28日 06:19
    Hello,

    Looks like in Test Class request parameter names are not as expected(in Rest Service Class), replace it with below and try: 

    req.params.put('emailId', 'sschimpf@urbn.com');

    req.params.put('brand', 'Anthropologie');  

    Thanks
0/9000