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..!!!