0

I need to test the method that needs a scanner input. I did try the other post which involves the same problem but it doesn't work for me I don't know why. It asks for bank branch name to be created and I want to check if it is created with the right name. But it is just stuck waiting for a user input. Thank you in advance.

    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.");
    }

Here is the test block I tried from searching various issues within 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));
    }
Emma
  • 27,428
  • 11
  • 44
  • 69
Mikko
  • 1
  • You need of [Mockito](https://site.mockito.org/) – Renato Oct 18 '20 at 07:18
  • any tips/guide for using Mockito @Renato – Mikko Oct 18 '20 at 07:29
  • Biggest tip: read the Mockito website which Renato gave you a link for. That's where I started a few month. It did confuse me a bit at first, but you do need to give it a go. Come back to us when you have specific questions. Bear in mind that you may need to refactor your createBranches() method to get it into a good state for testing (personally, I would split it into three separate methods). Refactoring is NOT a pain in the rear end, even if it feels like it at the time. It is a path to having better code, and to being a better programmer :) – sunrise Oct 18 '20 at 08:28
  • @sunrise so I need to refactor my whole code with the addition of me learning Mockito, noted thank you for the suggestion. – Mikko Oct 18 '20 at 08:38
  • @Mikko When you use `setIn()`, you have to put your test call between the `setIn()` statements. So it's like "Change `setIn()`, call your method, change it back". It doesn't make sense to change `setIn()`, then change it back again, and then do your test call. In that case you could skip the `setIn()` calls all together as they don't have any impact on your test calls. – Progman Oct 18 '20 at 09:49

0 Answers0