Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by tschaefer ( 4 years ago )
@isTest
private class AccountInformationTest {

    private static final Id accountId;

    private static final Integer numberOfEmployees;

    private static final String mockedMethodName;

     // system under test
    static {
        accountId = Id.valueOf('0017Q000007RayhQAC');
        numberOfEmployees = 20000;   
        mockedMethodName = 'getAccountEmployeeAmountFor';
    }

    /** Mock Framework FFLib ApexMocks Framework */
    @isTest
    private static void testCanRetrieveAccountInformationByGivenAccountId() {

        fflib_ApexMocks mocks = new fflib_ApexMocks();
        accountService accountServiceMock = (accountService) mocks.mock(accountService.class);

        mocks.startStubbing();
        mocks
         .when(accountServiceMock.getAccountEmployeeAmountFor(accountId))
         .thenReturn(numberOfEmployees);
        mocks.stopStubbing();

        Test.startTest();
          AccountInformation subject = new AccountInformation(accountServiceMock);
        Test.stopTest();

        System.assertEquals(
         'Number of employees (' + numberOfEmployees + ') for company ' + accountId,
         subject.getInformation(accountId)
       );
    }

    @isTest
    private static void testCanRetrieveAccountInformationByGivenAccountIdWithAmossTestFramework() {

        Id accountId = Id.valueOf('0017Q000007RayhQBD');
        Integer numberOfEmployees = 35000;

        Amoss_Instance accountServiceMock = new Amoss_Instance(AccountService.class);
        accountServiceMock
          .when (mockedMethodName)
          .withParameter(accountId)
          .willReturn(numberOfEmployees);

        AccountService accountServiceMockDouble = (AccountService) accountServiceMock.getDouble();

        Test.startTest();
          AccountInformation subject = new AccountInformation(accountServiceMockDouble);
        Test.stopTest();

        accountServiceMock.verify();

        System.assertEquals(
         'Number of employees (' + numberOfEmployees + ') for company ' + accountId,
          subject.getInformation(accountId)
      );
    }

    /** Mock Framework Amoss */
    @isTest
    private static void testCanNotRetrieveAccountInformationForForbiddenAccountId() {

        Id accountId = Id.valueOf('0017Q000007RayhQTS');
        String expectedExceptionMessage = 'the account (' + accountId + ') id is not allowed for the account information request';

        Amoss_Instance accountServiceMock = new Amoss_Instance(AccountService.class);
        accountServiceMock
          .when(mockedMethodName)
          .withParameter(accountId)
          .willThrow(new CustomAccountService.ForbiddenAccountIdException(expectedExceptionMessage));

        AccountService accountServiceMockDouble = (AccountService) accountServiceMock.getDouble();

        accountServiceMock.verify();

        Test.startTest();
          AccountInformation subject = new AccountInformation(accountServiceMockDouble);
          try {
            subject.getInformation(accountId);
          } catch (CustomAccountService.ForbiddenAccountIdException thrownException) {
            System.assertEquals(expectedExceptionMessage, thrownException.getMessage());
          }
        Test.stopTest();
    }

    /** Mock Framework Apex Universal Mocker */
    @isTest
    private static void testtestCanRetrieveAccountInformationByGivenAccountIdWithUniversalTestFramework() {

        UniversalMocker mockService = UniversalMocker.mock(AccountService.class);
        AccountService accountServiceMock = (AccountService)mockService.createStub();

        mockService
          .when(mockedMethodName)
          .thenReturn(numberOfEmployees);        

        Test.startTest();
          AccountInformation subject = new AccountInformation(accountServiceMock);
          subject.getInformation(accountId);
        Test.stopTest();

        mockService.assertThat().method(mockedMethodName).wasCalled(1);
        System.assertEquals(
          'Number of employees (' + numberOfEmployees + ') for company ' + accountId,
          subject.getInformation(accountId)
       );
    }

}

 

Revise this Paste

Parent: 121271
Your Name: Code Language: