function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Natalya MurphyNatalya Murphy 

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); }

    }
}

 
Shruti SShruti S
Could you please give me the line number in which the exception occurs?
Natalya MurphyNatalya Murphy
Class.AccountManager.getAccount: line 15, column 1
Class.AccountManagerTest.testGetAccountById: line 18, column 1
Shruti SShruti S
Alright. Now let me tell you what went wrong. You should not be hard coding the Account Id in the Test Class, instead you should be creating a test Account record in the Test Class and use that Id.

You can try out the below code - 
@isTest

public class AccountManagerTest {

    private static Account createTestAccount() {
        Account acc = new Account();
        acc.Name = 'ABC Corp';
        INSERT acc;
    }  

    @isTest static void testGetAccountById(){
        // Set up a test request
        RestRequest request = new RestRequest();

         //Creating Test Data
        String testAcctId = createTestAccount().Id;

        // Set request properties
        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); }
    }
}