0

我需要测试需要扫描仪输入的方法。我确实尝试了涉及相同问题的另一篇文章,但它对我不起作用,我不知道为什么。它要求创建银行分行名称,我想检查它是否使用正确的名称创建。但它只是等待用户输入。先感谢您。

    protected static void createBranches() throws Exception {
        String branchName;
        boolean check = true;
        do {
            check = false;
            System.out.println("Please enter the branch name: ");
            branchName = scanner.nextLine();
            for (int x = 0; x < branches.size(); x++) {
                if (branchName.equals(branches.get(x).getBranchName())) {
                    System.out.println("Branch name already exist, Please use a unique one.");
                    check = true;
                }
            }
        } while (check);
        Branches branch = new Branches(branchName);
        branches.add(branch);
        System.out.println("Branch successfully created.");
    }

这是我在stackoverflow中搜索各种问题时尝试的测试块。

    public void bankTest() throws Exception {
        Bank bank = new Bank();
        String input = "UnionBank";
        InputStream stdin = System.in;
        try {
            System.setIn(new ByteArrayInputStream(input.getBytes()));
            Scanner scanner = new Scanner(System.in);
            System.out.println(scanner.nextLine());
        } finally {
            System.setIn(stdin);
            bank.createBranches();
        }
        Assert.assertEquals("UnionBank", bank.branches.get(0));
    }
4

0 回答 0