SOQL Mocking

mail@pastecode.io avatar
unknown
java
a year ago
1.6 kB
0
Indexable
Never
public inherited sharing class SOQL_Account extends SOQL implements SOQL.Selector {
    public static SOQL_Account query() {
        return new SOQL_Account();
    }

    private SOQL_Account() {
        super(Account.SObjectType);
        // default settings
        with(Account.Id, Account.Name, Account.Type)
            .systemMode()
            .withoutSharing();
    }

    public SOQL_Account byRecordType(String rt) {
        whereAre(Filter.recordType().equal(rt));
        return this;
    }

    public SOQL_Account byName(String name) {
        whereAre(Filter.name().equal(name));
        return this;
    }
}

public with sharing class ExampleController {

    public static List<Account> getPartnerAccounts(String accountName) {
        
        // Your additional logic here 
        // ...
        
        return SOQL_Account.query()
            .byRecordType('Partner')
            .byName(accountName)
            .with(Account.BillingCity, Account.BillingCountry)
            .mockId('ExampleController.getPartnerAccounts')
            .toList();
    }
}

@IsTest
private class ExampleControllerTest {

    @IsTest
    static void getPartnerAccounts() {
        List<Account> accounts = new List<Account>{
            new Account(Name = 'MyAccount 1'),
            new Account(Name = 'MyAccount 2')
        };

        SOQL.setMock('ExampleController.getPartnerAccounts', accounts);

        // Test
        List<Account> result = ExampleController.getPartnerAccounts('MyAccount');

        Assert.areEqual(accounts, result);
    }
}