
本教程将指导如何在java中为包含用户输入(通过scanner)和字符串比较逻辑的服务方法编写单元测试。我们将重点介绍如何利用mockito模拟scanner以隔离外部依赖,确保测试的独立性和可重复性,并通过具体的代码示例展示测试数据的准备、模拟行为的配置以及结果的验证。
在Java应用开发中,服务层方法经常需要与用户交互,例如通过控制台读取输入。考虑以下一个更新菜单项的服务方法 updateMenu:
// 辅助类定义
class DailyMenu {
private List<MenuItem> menuItemList = new ArrayList<>();
public List<MenuItem> getMenuItemList() { return menuItemList; }
public void setMenuItemList(List<MenuItem> menuItemList) { this.menuItemList = menuItemList; }
}
class MenuItem {
private String names;
private String description;
private String image;
private float price;
public MenuItem() {}
public MenuItem(String names, String description, String image, float price) {
this.names = names;
this.description = description;
this.image = image;
this.price = price;
}
public String getNames() { return names; }
public void setNames(String names) { this.names = names; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public String getImage() { return image; }
public void setImage(String image) { this.image = image; }
public float getPrice() { return price; }
public void setPrice(float price) { this.price = price; }
@Override
public String toString() {
return "MenuItem{" +
"names='" + names + '\'' +
", description='" + description + '\'' +
", image='" + image + '\'' +
", price=" + price +
'}';
}
}
interface MenuPrinter {
void printMenu(DailyMenu dailyMenu);
}
// 原始服务方法所在的类
public class MenuService {
private final Scanner scanner;
private final MenuPrinter menuPrinter;
// 假设 Scanner 和 MenuPrinter 通过构造函数注入
public MenuService(Scanner scanner, MenuPrinter menuPrinter) {
this.scanner = scanner;
this.menuPrinter = menuPrinter;
}
public void updateMenu(DailyMenu dailyMenu) {
try {
menuPrinter.printMenu(dailyMenu);
System.out.print("Insert menu item want to update:");
String changeItem = scanner.nextLine(); // 读取用户输入的菜单项名称
dailyMenu.getMenuItemList().forEach((MenuItem menuItem)->{
// 核心逻辑:字符串比较
if(menuItem.getNames().equals(changeItem)){
System.out.print("\nInsert name: ");
menuItem.setNames(scanner.nextLine());
System.out.print("Insert description: ");
menuItem.setDescription(scanner.nextLine());
System.out.print("Insert image: ");
menuItem.setImage(scanner.nextLine());
System.out.print("Insert price:");
menuItem.setPrice(scanner.nextFloat());
}
});
} catch (NullPointerException | IllegalStateException exception){
System.err.println(exception.getMessage()); // 使用 System.err 打印错误
} catch (InputMismatchException exception){
System.err.print(new InputMismatchException("Menu item quantity must be number !!!").getMessage());
}
}
}这个 updateMenu 方法的功能是:显示当前菜单,然后提示用户输入要更新的菜单项名称。如果找到匹配项(通过 menuItem.getNames().equals(changeItem) 进行字符串比较),则会再次提示用户输入新的名称、描述、图片和价格,并更新对应的 MenuItem 对象。
以上就是使用Mockito为Java服务方法中包含用户输入的字符串比较逻辑编写单元测试的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号