クラスとそのテストクラスを下記のように実装し
確認するたびにエラーが発生
List has no rows for assignment to SObject
最後解決した方法が
@RestResource(urlMapping='/Account//contacts')
↓
@RestResource(urlMapping='/Accounts//contacts')
要は複数に変更し、確認すれば、sfdc trailheadが通してくれた。
ご参考まで
#java
@RestResource(urlMapping='/Accounts/*/contacts')
global with sharing class AccountManager {
@HttpGet
global static Account getAccount() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String accountId = req.requestURI.substringBetween('Accounts/', '/contacts');
Account acc = [SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account WHERE Id = :accountId];
return acc;
}
}
#java
@istest
public class AccountManagerTest {
@isTest static void testGetAccount() {
Id recordId = createTestRecord();
// Set up a test request
RestRequest request = new RestRequest();
request.requestUri =
'https://ap5.salesforce.com/services/apexrest/Accounts/'+recordId+'/contacts';
request.httpMethod = 'GET';
RestContext.request = request;
// Call the method to test
Account thisAcc = AccountManager.getAccount();
// Verify results
System.assert(thisAcc != null);
System.assertEquals('Test record', thisAcc.Name);
}
// Helper method
static Id createTestRecord() {
// Create test record
Account accTest = new Account(
Name='Test record');
insert accTest;
Contact con = new Contact();
con.LastName = 'test con';
con.Account = accTest;
insert con;
return accTest.Id;
}
}