【发布时间】:2015-09-04 14:32:26
【问题描述】:
我在为 Spring 项目编写 Junit 测试用例时遇到问题。它关于以下方法;
boolean doesUserIdExist(String userId){
if(userRepository.findOne(userId.toLowerCase()) != null) {
throw new userAlreadyExistsException("User with id: " + userId + " already exists")
return false;
}else{
return true;
}
}
现在在我的 jUnit 中我写了这样的东西..:
void compareDuplicateUserIdTest (){
UserService UserService = new UserService();
String lowercase = "test";
String uppercase = "Test";
boolean result = userService.doesUserIdExist(lowercase);
//Check the boolean result if its true
}
由于我使用 findOne 方法,这意味着我必须对照 DB userId = “test”检查 String = “test”。这不是正确的方法,因为它应该在 MongoDB 数据库中没有任何记录的情况下独立工作。
现在我一直在阅读有关 mockito 之类的框架来测试它,但是对于如此简单的方法检查来说,这不是“太多”了吗?我可以删除 findOne 部分并只比较字符串吗?
【问题讨论】: