This is the service to test :
public void method(List<String> inputs) { Map<String,Object> parameterMap = new HashMap<>(); parameterMap.put("common_attr1","blah"); parameterMap.put("common_attr2","blah"); for(String input: inputs) { parameterMap.put("selector",input); dao.makeDBCall(parameterMap); } }
To collect the argument passed to the DB call, you would ideally go for the ArgumentCaptor and see if each call is made with the corresponding input passed to the method()
But, this will not work because ArgumentCaptor gives you the reference. so all the captured value will point to the argument corresponding to the last call made to the DB call.
Solution :
Use thenAnswer to collect the parameters while invoking the DB call.List<String> actualSelectorsPassedForInputs = new ArrayList<>(); when(dao.makeDBCall(anyObject())).thenAnswer(invocation->{ Object[] args = invocation.getArguments(); HashMap<String,Object> parameterMap = (HashMap<String,Object>) args[0]; actualSelectorsPassedForInputs.add(parameterMap.get("selector")); }); // do assertions on actualSelectorsPassedForInputs
Reference :
1) https://stackoverflow.com/questions/11984128/capturing-previous-values-to-verify-mock-object