Example of an Apex class to get account details and its corresponding test class

public class AccountDetails {
    // Method to retrieve account details based on account Id
    public static Account getAccountDetails(Id accId) {
        return [SELECT Id, Name, Industry, Type, Phone, Website FROM Account WHERE Id = :accId];
    }
}


Here's the corresponding test class:

@isTest
public class AccountDetailsTest {
    // Test method to verify account details retrieval
    @isTest
    static void testGetAccountDetails() {
        // Create test account
        Account testAccount = new Account(Name='Test Account', Industry='Technology', Type='Customer', Phone='1234567890', Website='www.example.com');
        insert testAccount;

        // Retrieve the test account details using the method
        Account retrievedAccount = AccountDetails.getAccountDetails(testAccount.Id);

        // Verify if the retrieved account details match the expected values
        System.assertEquals(testAccount.Name, retrievedAccount.Name);
        System.assertEquals(testAccount.Industry, retrievedAccount.Industry);
        System.assertEquals(testAccount.Type, retrievedAccount.Type);
        System.assertEquals(testAccount.Phone, retrievedAccount.Phone);
        System.assertEquals(testAccount.Website, retrievedAccount.Website);
    }
}

Leave a Reply

Back To Top