I am having difficulty in getting a test to work using Spock Framework in a Java project. I have the following:
- Person.class
- Attribute.class
I have a service class which is being mocked in my test. The method I am trying to mock has the following signature:
serviceClass.call(Map<Person, List<Attribute>> map)
When I mock it purely using wildcards like:
serviceClass.call(_) >> MockReturnObject
Everything works as expected. The call in the service class will return the MockReturnObject.
However, for my specific case I need to specify the Person object I am passing in and assign it a specific MockReturnObject. Like:
serviceClass.call([(PersonA):_)]) >> MockReturnObjectA
or
def listWildcard = _
serviceClass.call(Map.of(PersonA, listWildcard)) >> MockReturnObjectA
Neither of these approaches work and the call ends up returning null instead of MockReturnObjectA (I assume it is because it is failing to match the arguments). I am unfortunately not too experienced with Spock and my attempt to search through documentation on handling Maps in this scenario has not come up fruitful. I would appreciate anyone able to offer any guidance.
I don't think it makes a difference but PersonA is passed in to an entry method in the serviceClass in a list like;
List<Attribute> list = getAttributeList()
entryClass.process(List<Person> personList) {
personList.forEach(person -> serviceClass.call(Map.of(person, list))
}
So in my tests, the "when" is:
entryClass.process([PersonA, PersonB, PersonC])
With all 3 being Mock(Person.class) with their own behaviours.