如何使用JUnit 4在Java控制台应用程序中测试循环的mainManu方法?

时间:2019-04-06 18:37:43

标签: java testing junit

我必须准备一个带有一些测试用例的控制台“销售点”应用程序。我制作了一个销售点控制器,该控制器包含mainManu样式的方法processSales,其中包含2个循环。不知道如何为它编写一些简单的测试用例。任何建议将不胜感激。

我试图在processSales方法中重构一些代码以测试用户的输入,但是对我来说似乎毫无意义,只要这些方法不是简单的返回类型方法,而是它们可以控制外部类的流程..

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

1 个答案:

答案 0 :(得分:0)

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.Assert;
import org.junit.Test;
...
@Test
public void test()  {
  String input = "INPUT";
  String expected = "OUTPUT";

  // set stdin
  System.setIn(new ByteArrayInputStream(input.getBytes()));

  // set stdout
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  PrintStream ps = new PrintStream(baos);
  System.setOut(ps);

  // call the method that reads from stdin and writes to stdout
  ...

  // assert stdout's content value
  Assert.assertEquals(expected, baos.toString());
}