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

标签 java testing junit

我必须准备一个带有一些测试用例的控制台“销售点”应用程序。我制作了销售点 Controller ,其中包含 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);
            }
        }
    }

最佳答案

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

关于java - 如何使用 JUnit 4 在 Java 控制台应用程序中测试循环的 mainManu 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55552280/

相关文章:

testing - 如何在 linux 服务器中暂停以非 gui 模式运行的 Jmeter

java - org.mockito.exceptions.base.MockitoException : Please ensure that the type 'UserRestService' has a no-arg constructor

maven-2 - Junit 运行时不选择文件 src/test/resources。对于某些依赖 jar 所需的文件

java - 简单的@ManyToOne 关系 : could not resolve property (org. hibernate.QueryException)

java - 我想获得java中员工可用的休假数量

java - 如何使用Java将字符串和二进制文字发送到服务器

junit - 如何在 IBM J9 JVM 上将 JMockit 与 Gradle 一起使用?

java - 嵌套类中的公共(public)构造函数有什么用

grails - 如何在功能上测试依赖的Grails应用程序

testing - 获取 oauth 凭据!