From bd268547384ec81ea54b1bcbc387d9fd1cf97a7d Mon Sep 17 00:00:00 2001 From: rygramer Date: Tue, 26 Sep 2023 08:16:14 -0400 Subject: [PATCH 1/2] bug: reproduce error using sample code --- .../test/classes/AccountSloganRelatedTest.cls | 49 +++++++++++++++++++ .../test/classes/SomethingIsAwry.cls | 8 +++ .../test/classes/SomethingIsAwry.cls-meta.xml | 5 ++ 3 files changed, 62 insertions(+) create mode 100644 sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls create mode 100644 sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls-meta.xml diff --git a/sfdx-source/reference-implementation-marketing/test/classes/AccountSloganRelatedTest.cls b/sfdx-source/reference-implementation-marketing/test/classes/AccountSloganRelatedTest.cls index 8911882..3856bae 100644 --- a/sfdx-source/reference-implementation-marketing/test/classes/AccountSloganRelatedTest.cls +++ b/sfdx-source/reference-implementation-marketing/test/classes/AccountSloganRelatedTest.cls @@ -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 testRecords = new List(); + 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{ 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 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 + } } diff --git a/sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls b/sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls new file mode 100644 index 0000000..7fa3f98 --- /dev/null +++ b/sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls @@ -0,0 +1,8 @@ +public with sharing class SomethingIsAwry { + public List run(String slogan){ + SelectBySloganSelectorMethod.Parameters queryParams = new SelectBySloganSelectorMethod.Parameters(); + queryParams.sloganNameSet = new Set{slogan}; + + return AccountsSelector.newInstance().selectInjection( SelectBySloganSelectorMethod.class, queryParams); + } +} \ No newline at end of file diff --git a/sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls-meta.xml b/sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls-meta.xml new file mode 100644 index 0000000..642d054 --- /dev/null +++ b/sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls-meta.xml @@ -0,0 +1,5 @@ + + + 58.0 + Active + \ No newline at end of file From 03acc3a0d43231eebb7f27ab7d4fd3f22b7c592b Mon Sep 17 00:00:00 2001 From: rygramer Date: Tue, 26 Sep 2023 12:28:48 -0400 Subject: [PATCH 2/2] feat: add equals and hashcode impl to parameterable --- .../SelectBySloganSelectorMethod.cls | 12 +++++ .../test/classes/AccountSloganRelatedTest.cls | 49 ------------------- .../test/classes/SomethingIsAwry.cls | 8 --- .../test/classes/SomethingIsAwry.cls-meta.xml | 5 -- 4 files changed, 12 insertions(+), 62 deletions(-) delete mode 100644 sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls delete mode 100644 sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls-meta.xml diff --git a/sfdx-source/reference-implementation-marketing/main/classes/selectors/SelectBySloganSelectorMethod.cls b/sfdx-source/reference-implementation-marketing/main/classes/selectors/SelectBySloganSelectorMethod.cls index 8d381de..a72246b 100644 --- a/sfdx-source/reference-implementation-marketing/main/classes/selectors/SelectBySloganSelectorMethod.cls +++ b/sfdx-source/reference-implementation-marketing/main/classes/selectors/SelectBySloganSelectorMethod.cls @@ -24,5 +24,17 @@ public inherited sharing class SelectBySloganSelectorMethod implements ISelectorMethodParameterable { public Set sloganNameSet; + + public Boolean equals(Object obj){ + if(obj instanceOf SelectBySloganSelectorMethod.Parameters){ + SelectBySloganSelectorMethod.Parameters params = (SelectBySloganSelectorMethod.Parameters)obj; + return sloganNameSet.equals(params.sloganNameSet); + } + return false; + } + + public Integer hashCode(){ + return sloganNameSet.hashCode(); + } } } diff --git a/sfdx-source/reference-implementation-marketing/test/classes/AccountSloganRelatedTest.cls b/sfdx-source/reference-implementation-marketing/test/classes/AccountSloganRelatedTest.cls index 3856bae..8911882 100644 --- a/sfdx-source/reference-implementation-marketing/test/classes/AccountSloganRelatedTest.cls +++ b/sfdx-source/reference-implementation-marketing/test/classes/AccountSloganRelatedTest.cls @@ -101,53 +101,4 @@ 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 testRecords = new List(); - 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{ 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 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 - } } diff --git a/sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls b/sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls deleted file mode 100644 index 7fa3f98..0000000 --- a/sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls +++ /dev/null @@ -1,8 +0,0 @@ -public with sharing class SomethingIsAwry { - public List run(String slogan){ - SelectBySloganSelectorMethod.Parameters queryParams = new SelectBySloganSelectorMethod.Parameters(); - queryParams.sloganNameSet = new Set{slogan}; - - return AccountsSelector.newInstance().selectInjection( SelectBySloganSelectorMethod.class, queryParams); - } -} \ No newline at end of file diff --git a/sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls-meta.xml b/sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls-meta.xml deleted file mode 100644 index 642d054..0000000 --- a/sfdx-source/reference-implementation-marketing/test/classes/SomethingIsAwry.cls-meta.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - 58.0 - Active - \ No newline at end of file