首页 > Java > java教程 > 正文

使用Mockito为Java服务方法中包含用户输入的字符串比较逻辑编写单元测试

聖光之護
发布: 2025-10-22 12:09:26
原创
588人浏览过

使用Mockito为Java服务方法中包含用户输入的字符串比较逻辑编写单元测试

本教程将指导如何在java中为包含用户输入(通过scanner)和字符串比较逻辑的服务方法编写单元测试。我们将重点介绍如何利用mockito模拟scanner以隔离外部依赖,确保测试的独立性和可重复性,并通过具体的代码示例展示测试数据的准备、模拟行为的配置以及结果的验证。

1. 理解待测试方法及其挑战

在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 对象。

AI图像编辑器
AI图像编辑器

使用文本提示编辑、变换和增强照片

AI图像编辑器 46
查看详情 AI图像编辑器

以上就是使用Mockito为Java服务方法中包含用户输入的字符串比较逻辑编写单元测试的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号