You need to sign in to do that
Don't have an account?
Data comes back from workbench but not from test run
This is for the trailhead unit "Apex Web Services." When I call the service from the workbench using an account ID, I get the expected response back. But when I call the same service from within a test class with that same account ID, I get an exception that says "List has no rows for assignment to SObject", meaning no data is returned from the query. Any ideas? All code is shown below.
@RestResource(urlMapping='/Accounts/*/contacts') global with sharing class AccountManager { @HttpGet global static Account getAccount(){ RestRequest request = RestContext.request; System.debug('request: ' + request.requestURI); String[] splitURI = request.requestURI.split('/'); System.debug(splitURI); String acctId = splitURI[splitURI.size() - 2]; System.debug( 'id: ' + acctId); Account acct = null; acct = [SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account WHERE Id =: acctId]; if( acct != null ) {System.debug(acct);} return acct; } }
@isTest public class AccountManagerTest { @isTest static void testGetAccountById(){ // Set up a test request RestRequest request = new RestRequest(); // Set request properties String testAcctId = '0014600000KcFRn'; request.requestUri = URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/Accounts/' + testAcctId + '/contacts'; request.httpMethod = 'GET'; System.debug('URI: ' + request.requestURI); RestContext.request = request; sObject testAcct = null; testAcct = AccountManager.getAccount(); System.assert(testAcct != null); if( testAcct != null ) { System.assertEquals(testAcctId, testAcct.Id); } } }
Class.AccountManagerTest.testGetAccountById: line 18, column 1
You can try out the below code -