有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

maven Java命令解释器测试

目标很简单。能够测试一个非常简单的命令解释器

我目前正在使用TestNG测试一个根目录中有命令解释器的Java应用程序。但我不能做到这一点

一旦应用程序启动,它就会进入一个循环,该循环处理用户输入并选择从那里开始的位置(),并且只有当用户的输入等于“exit”时才会停止。方法打印所有可用的命令

您能告诉我应该如何继续测试printHelp()方法以模拟用户的输入并获得正确的输出吗

注意:假设printHelp()方法应该打印字符串“所有需要的信息”

下面提供了maven依赖项和主类

pom。xml

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.0.0</version>
    <scope>test</scope>
</dependency>

Java主类

public static void main(String args[]){
    menu();
}
private static void menu() {
    Scanner in = new Scanner(System.in);
    String[] fullLine;
    StringConst.CMDS cmd; //enum of prossible cmds

    do {
        System.out.print(StringConst.PROMPT); //aesthetic
        fullLine = in.nextLine().toUpperCase().split(" ");
        cmd = StringConst.CMDS.fromString(fullLine[0]);

        switch (cmd) {
            (...)
            case HELP:
                printHelp();
                break;
            case EXIT:
                System.out.println("Exiting...");
                break;
            default:
                System.err.println(StringConst.NOT_FOUND);
                break;
        }

    } while (!cmd.equals(StringConst.CMDS.EXIT));
}

private static void printHelp() {
    for (StringConst.CMDS cmd : StringConst.CMDS.values()) {
        System.out.println(cmd + " - " + cmd.getDescription());
    }
}

StringConst类

public class StringConst {

    public static final String NOT_FOUND = "Command not found... Please try again.";
    public static final String PROMPT = "> ";

    public enum CMDS {
        HELP, EXIT, OTHER;

        private String description;

        static {
            HELP.description = "Prints each command's function";
            EXIT.description = "Exits the program";
        }

        public String getDescription() {
            return description;
        }

        public static CMDS fromString(String temp) {
            return CMDS.valueOf(temp);
        }
    }
}

共 (0) 个答案