Skip to content

Commit

Permalink
bug: reproduce error using sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
rygramer committed Sep 26, 2023
1 parent 90b5d9d commit bd26854
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,53 @@ private class AccountSloganRelatedTest
System.debug('accountsQueried size: ' + accountsQueried.size());
((IAccountsSelector)mocks.verify(mockAccountsSelector)).selectInjection( SelectBySloganSelectorMethod.class, queryParams);
}

@isTest
static void givenNewAccountWhenCreatedWithFishNameThenSelectorMethodInjectionUsingADifferentClass()
{
// given
fflib_ApexMocks mocks = new fflib_ApexMocks();

String slogan = 'The Big Blue Fish is a fishy business';

// Setup the test record to be returned by the mock selector
Account bluefishAccount = new Account();
bluefishAccount.Id = fflib_IDGenerator.generate( Account.SObjectType );
bluefishAccount.name = 'bluefish';
bluefishAccount.slogan__c = slogan;

List<Account> testRecords = new List<Account>();
testRecords.add(bluefishAccount);

// Setup the mocks needed for the test
IAccountsSelector mockAccountsSelector = (IAccountsSelector) mocks.mock( IAccountsSelector.class );

// Setup the injection parameter class
SelectBySloganSelectorMethod.Parameters queryParams = new SelectBySloganSelectorMethod.Parameters();
queryParams.sloganNameSet = new Set<String>{ slogan };

// Stub the mocks
mocks.startStubbing();

fflib_MethodReturnValue mockAccountsSelectorMethodReturnValue = mocks.when(mockAccountsSelector.selectInjection(SelectBySloganSelectorMethod.class, queryParams));
mockAccountsSelectorMethodReturnValue.thenReturn(testRecords);
mocks.when(mockAccountsSelector.sObjectType()).thenReturn(Account.SObjectType);

mocks.stopStubbing();

Application.Selector.setMock(mockAccountsSelector);

// when
Test.startTest();

List<Account> accountsQueried = new SomethingIsAwry().run(slogan);

Test.stopTest();

// then
System.debug('accountsQueried size: ' + accountsQueried.size());
((IAccountsSelector)mocks.verify(mockAccountsSelector)).selectInjection( SelectBySloganSelectorMethod.class, queryParams);
// fails with
// System.NullPointerException: Attempt to de-reference a null object // Class.AccountSloganRelatedTest.givenNewAccountWhenCreatedWithFishNameThenSelectorMethodInjectionUsingADifferentClass: line 148, column 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public with sharing class SomethingIsAwry {
public List<Account> run(String slogan){
SelectBySloganSelectorMethod.Parameters queryParams = new SelectBySloganSelectorMethod.Parameters();
queryParams.sloganNameSet = new Set<String>{slogan};

return AccountsSelector.newInstance().selectInjection( SelectBySloganSelectorMethod.class, queryParams);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit bd26854

Please sign in to comment.