PDA

View Full Version : The testGetAccounts method doesn’t seem to find any records, even though I expect it



Cathlilly
12-03-2024, 10:18 PM
I’m writing a test class using the @IsTest annotation to validate my Apex method that retrieves a list of accounts. However, the test keeps failing with a "System.QueryException: List has no rows for assignment to SObject" error. It seems like the test class isn’t recognizing the data I inserted during the test setup. Here’s the code I’ve written:


@IsTest
private class AccountTest {
@IsTest
static void testGetAccounts() {
// Test the getAccounts method
List<Account> accounts = [SELECT Id, Name FROM Account];
System.assert(accounts.size() > 0, 'No accounts were returned');
}
}

The testGetAccounts method doesn’t seem to find any records, even though I expect it to retrieve the data. How can I fix this issue and properly test my method using the @IsTest annotation?