0

Hi All,

I'm unable to fix the code and giving the error as "FATAL_ERROR System.QueryException: List has no rows for assignment to SObject " in test class. I have implemented Apex class for rest API but having the issue with test Class. Could you some one help me on this?

See Below apex class code,

@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 is below

@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', '[email protected]');
        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&[email protected]';
         //req.requestURI= URL.getOrgDomainUrl() +'/services/apexrest/GetContactId';
         req.requestURI= '/services/apexrest/GetContactId';
         RestContext.request = req;
         RestContext.response = res;
         string results = PersonContactRestEndpoint.getPersonContactId(); 
        Test.stopTest();
        
    }
}

Is I'm missing anything? Thanks in advance for the help..!!!

1 Answer 1

0

Well, in your test class you create an account

Account acc = new Account();
acc.Name='Test';
acc.AccountNumber ='12345';
insert acc;

but in the class under test you search for an account that is a person account and has to have PersonEmailset. As you haven't created a matching account, in the line

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

there is no result, which creates an Exception.

Always first store all possible results in a list, and then check if there are actual results - you would always never be sure there will be a result.

So, first change the class under test to

List<Account> customers=  [SELECT PersonContactId FROM Account WHERE CC_Brand__c = :Brand AND PersonEmail = :EmailId LIMIT 1];
if (customers.size () > 0) {
    ... your code here ...

and second, do create a matching account in your test class, so that the class under test actually can find a result and work with that.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.