Junit mockito when (..). ThenReturn () throws a NullPointerException

Can someone explain the script below for testing code
UserTransaction.java

@Override public ServiceResponse<User> get(String name) { ServiceResponse<User> response = new ServiceResponse<User>(); List<Map<String, Object>> exp = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("expression", "eq"); map.put("property", "name"); map.put("value", name); exp.add(map); List<User> users = userDao.getByCriteria(exp); if (!users.isEmpty()) { response.setResponse(users.get(0)); } else { response.setResponse(null); } return response; } 

UserDao.java

 public List<User> getByCriteria(List<Map<String, Object>> exp) { DetachedCriteria criteria = DetachedCriteria.forClass(User.class); for (Integer i=0;i<exp.size();i++){ String expression = (String) exp.get(i).get("expression"); String property = (String) exp.get(i).get("property"); if(expression.equals("eq"){ criteria.add(Restrictions.eq(property,exp.get(i).get("value"))); } } return hibernateTemplate.findByCriteria(criteria); } 

UserTransactionTest.java

 private UserTransaction userTransactions = new UserTransaction(); private UserDao userDao = mock(UserDao.class); @Test public void testGet() { User user = new User(); user.setName("Raman"); try { when(userDao.getByCriteria(anyList())).thenReturn(user); } catch (Exception e) { e.printStackTrace(); } ServiceResponse<User> response = userTransactions.get("raman"); User result = response.getResponse(); assertEquals("Raman", result.getName()); assertEquals(0, response.getErrors().size()); } 

works great.

But instead of "anyList ()" I passed a custom list of "myList"

 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); when(userTransactions.getByCriteria(myList)).thenReturn(user); 

Throws a NullPointerException on the assertEquals() . What for? What actually happens if anyList() is specified?

+6
source share
4 answers

If your code is complete (I suspect this may not be the case), you did not specify a mock object containing the get() method. This must be present in the call to when(...)

I expect such a code ...

 UserDao mockDao = mock(UserDao.class); when(mockDao.get(list)).thenReturn(users); 
+4
source

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()); } 
+4
source

I think anyList () is a method that you are mocking, and the list is not a method, can you send the source code for everything you write this test case?

0
source

First of all, you are not testing UserDao.

Next, anyList () creates a mockito pairing, and you must pass a userDao.getByCriteria match to do something, so you should use Matchers.same (your list) or Matchers.eq (your list).

An exception occurs because, by default, Mockito creates a nice layout, and by default they return null on any unexpected method call.

0
source

Source: https://habr.com/ru/post/922936/


All Articles