Skip to content

How to mock AbstractSelectorMethodInjectable? #10

@rygramer

Description

@rygramer

Question

How do you mock an AbstractSelectorMethodInjectable where the class is called from another layer (like Service)?

The sample code shows how to do it when making the selectInjection call from the actual test. How do you do it when the selectInjection call is not in the test itself, but rather in the class under test?

For example, this test fails with a NullPointerException - rygramer@bd26854. (This is essentially a copy and paste from the sample code test, except for line 143.)

Answer

If you dont implement the equals method inside of the Parameters class, when you are creating another instance of the Parameters for testing the injection, that is using a memory reference to compare the objects
Those objects are not equal, thus the mock does not meet its criteria, thus it returns nothing, which you assign to your list
Which you then get a NPE because you are trying to access size of a null list
It worked in the sample because the same instance of the SelectBySloganSelectorMethod.Parameters was being used, thus it was equal
(obligatory RTFM reference -> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps_keys_userdefined.htm)
The docs are talking about Maps and Sets, but the overall concept is relevant here
np! For reference, we dont add the equals and hashCode methods for you, since we arent sure what attributes you are going to define for your Parameters

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions