use*_*577 7 java junit unit-testing mockito
任何人都可以解释我下面的场景
代码测试
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;
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
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());
}
Run Code Online (Sandbox Code Playgroud)
工作良好.
但是我没有通过"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);
Run Code Online (Sandbox Code Playgroud)
投掷NullPointerException线assertEquals().为什么?如果anyList()给出了实际发生的事情?
我相信你现在已经解决了你的问题,但如果有人遇到同样的问题,这就是答案:
在您提供的代码中,您没有使用myList您创建的模拟.该get()方法始终调用userDao.getByCriteria(exp)一个局部变量.
这就是为什么anyList()有效,而myList不是.
如果你也想测试的表达,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;
}
}
Run Code Online (Sandbox Code Playgroud)
在你的测试中:
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());
}
Run Code Online (Sandbox Code Playgroud)
小智 0
我认为 anyList() 是一种您正在嘲笑的方法,而 list 不是一种方法,您能否发布您正在编写此测试用例的任何内容的源代码
| 归档时间: |
|
| 查看次数: |
43948 次 |
| 最近记录: |