I am sure that you have already solved your problem, but if someone came across the same problem, here is the answer:
In the code you provided, you are not using the mocked myList you created. The get() method always calls userDao.getByCriteria(exp) , a local variable.
This is why anyList() works, but myList does not.
If you do want to test the expression, List<Map<String,Object>> exp should be a member of your class, not a local variable:
public class UserTransaction { private List<Map<String,Object>> exp; public UserTransaction() { // creating a default exp value Map<String, Object> map = new HashMap<String, Object>(); map.put("expression", "eq"); map.put("property", "name"); map.put("value", name); exp.add(map); } // getters and setters for exp public ServiceResponse<User> get(String name) { ServiceResponse<User> response = new ServiceResponse<User>(); List<User> users = userDao.getByCriteria(exp); if (!users.isEmpty()) { response.setResponse(users.get(0)); } else { response.setResponse(null); } return response; } }
And in your test:
private UserTransaction userTransactions = new UserTransaction(); private UserDao userDao = mock(UserDao.class); @Test public void testGet() { User user = new User(); user.setName("Raman"); // creating a custom expression List<Map<String,Object>> myList = new ArrayList<Map<String,Object>>(); Map<String,Object> map = new HashMap<String,Object>(); map.put("expression","eq"); map.put("property","name"); map.put("value","raman"); myList.add(map); // replacing exp with the list created userTransactions.setExp(myList); // return user when calling getByCriteria(myList) when(userDao.getByCriteria(myList)).thenReturn(user); ServiceResponse<User> response = userTransactions.get("raman"); User result = response.getResponse(); assertEquals("Raman", result.getName()); assertEquals(0, response.getErrors().size()); }
Tarek source share