samedi 6 avril 2019

How to test looped mainManu method in Java console application with JUnit 4?

I have to prepare a console 'Point of sale' application with some test cases. I've made and point of sale controller which contains mainManu-style method (processSales) with 2 loops inside. Have no idea how to write some simple test cases for it. Any suggestion would be appreciated.

I have tried to refactor some code in my processSales metohod to test input from user, but it seems pointless for me as long those method are not simply return type method, but they're controlling flow from outter class..

public void processSales() {

    PointOfSaleOption option = PointOfSaleOption.NEW_TRANSACTION;

    while (option != PointOfSaleOption.EXIT) {

        switch (option) {

            case NEW_TRANSACTION:
                try {

                    Scanner sc = new Scanner(System.in);
                    System.out.println("Scan code to process ... or type Exit");
                    pointOfSale.startNewTransaction();

                    while (sc.hasNextLine()) {
                        String tmpCode = sc.nextLine();

                        if (tmpCode.toLowerCase().equals("exit")) {
                            closeCurrentTransaction(pointOfSale);
                            option = PointOfSaleOption.NEXT_TRANS;
                            break;
                        } else if (tmpCode.isEmpty()) {
                            LCD.printMsg("Invalid bar code");
                        } else {
                            if (productFoundInDatabase(tmpCode) != null) {
                                Product productToAdd = productFoundInDatabase(tmpCode);
                                pointOfSale.getCurrentTransatcion().add(productToAdd);
                                LCD.printMsg(String.valueOf(productToAdd.getProductPrice()));
                            } else {
                                LCD.printMsg("Product not found");
                            }
                        }
                    }
                } catch (NumberFormatException ex) {
                    System.err.println("Wrong input. Only 4-digit number allowed. Try again ...");
                }
                break;

            case NEXT_TRANS:
                System.out.println("New transaction ? Type [Y]/[N]");

                Scanner beginTransaction = new Scanner(System.in);
                while (beginTransaction.hasNextLine()){
                    String chosenOption = beginTransaction.nextLine().toLowerCase();

                    if (chosenOption.equals("y")){
                        option = PointOfSaleOption.NEW_TRANSACTION;
                        break;
                    } else if (chosenOption.equals("n")) {
                        option = PointOfSaleOption.EXIT;
                        break;
                    } else {
                        System.out.println("Invalid answer. Try again.");
                        beginTransaction.nextLine();
                    }
                }
                break;
            case EXIT:
                System.out.println("Closing application ...");
                System.exit(0);
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire