[英]Junit mockito when(..).thenReturn() throws NullPointerException
Can anyone explain me the below scenario 任何人都可以解释下面的情况
Code to be tested 要测试的代码
UserTransaction.java 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 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 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 fine. 工作良好。
But instead of "anyList()" I passed a user-defined list "myList" 但是我没有通过“anyList()”来传递用户定义的列表“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 NullPointerException
at the line assertEquals()
. 在
assertEquals()
行引发NullPointerException
。 Why? 为什么? What actually happens if
anyList()
is given? 如果给出
anyList()
会发生什么?
I'm sure you've already solved your problem by now, but in case anyone stumbles upon the same issue, here's the answer: 我相信你现在已经解决了你的问题,但如果有人遇到同样的问题,这就是答案:
In the code you've provided, you are not using the mocked myList
you've created. 在您提供的代码中,您没有使用您创建的
myList
。 The get()
method always calls userDao.getByCriteria(exp)
, a local variable. get()
方法总是调用userDao.getByCriteria(exp)
,一个局部变量。
This is why anyList()
works, while myList
doesn't. 这就是
anyList()
工作原理,而myList
则不然。
If you do want to test the expression, List<Map<String,Object>> exp
should be a member of your class, not a local variable: 如果你想测试的表达,
List<Map<String,Object>> exp
应该是你的类,而不是一个局部变量的成员:
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());
}
If your code is complete (I suspect it may not be) then you haven't specified the mock object that contains the get()
method. 如果你的代码是完整的(我怀疑它可能不是),那么你没有指定包含
get()
方法的mock对象。 This should be present in the call to when(...)
这应该存在于
when(...)
的调用中
I am expecting code like this... 我期待这样的代码......
UserDao mockDao = mock(UserDao.class);
when(mockDao.get(list)).thenReturn(users);
我认为anyList()是一个你正在嘲笑的方法,list不是一个方法,请你发表这个测试用例的任何内容都可以发布源代码
First of all you are not testing UserDao. 首先,您没有测试UserDao。
Next, anyList() produce mockito matcher and you should pass matcher to userDao.getByCriteria in order to do something, so, you should use Matchers.same(your list) or Matchers.eq(your list). 接下来,anyList()生成mockito匹配器,您应该将matcher传递给userDao.getByCriteria以执行某些操作,因此,您应该使用Matchers.same(您的列表)或Matchers.eq(您的列表)。
Exception appear because by default Mockito creats nice mock and by default they returning null on any unexpected method invocation. 出现异常是因为默认情况下Mockito创建了很好的模拟,默认情况下,它们会在任何意外的方法调用时返回null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.