Java IO、输入流和输出流测试

标签 java testing io

我正在尝试创建一个简单的聊天程序。
最初我编写这段代码是为了将用户的输入消息保存到文本文件中,具体取决于给定的命令,但我对测试策略一无所知。 我应该只为 runUi() 编写测试吗?因为这个方法已经包含 switch 语句并且在它里面调用了其他方法,还是我应该为所有其他方法也写?

public class CmdLineUI implements ChatUI, Chat {
    public static final String WRITE = "write";
    public static final String READ = "read";
    public static final String EXIT = "exit";
    public static final String ERRORMESSAGE = "unknown command";

    private final PrintStream consoleOutput;
    private final BufferedReader userInput;

    public CmdLineUI(PrintStream os, InputStream is) {
        this.consoleOutput = os;
        this.userInput = new BufferedReader(new InputStreamReader(is));
    }

    public static void main(String[] args) throws Exception {
        PrintStream os = System.out;
        os.println("***Welcome to The ChatSystem:  input: write for chatting ,"
                + "\n    for Reading the messages input: read ,for closing Chat input: exit ***");

        CmdLineUI userCmd = new CmdLineUI(os, System.in);

        userCmd.runUi(System.in, os);
    }



    private String[] readCommands() throws Exception {
        String[] result = new String[1];
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String commandLinString = null;
        String command = null;

        try {
            System.out.println("> ");

            commandLinString = br.readLine();
            StringTokenizer st = new StringTokenizer(commandLinString);
            command = st.nextToken().trim();
            // msg = st.nextToken();

            System.out.println("command :" + command);
        } catch (IOException e) {
            System.out.println("Exception when reading from comman line" + e.getLocalizedMessage());
        }
        result[0] = command;
        // result[1] = msg;

        return result;
    }

    @Override
    public void writeMessage(String message) throws Exception {
        System.out.println("Start with Writing mood:");

        try {
            File file = new File("/home/sami/Desktop/file.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(System.in); // A stream for reading from the
                                                                                    // console
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader); // Connect InputStreamReader to a
                                                                                    // BufferedReader
            FileWriter fileReader = new FileWriter(file); // A stream that connects to the text file
            BufferedWriter bufferedWriter = new BufferedWriter(fileReader); // Connect the FileWriter to the
                                                                            // BufferedWriter
            String line;
            boolean continuee = true;

            while (!(line = bufferedReader.readLine()).equals("stop")) {
                continuee = false;
                bufferedWriter.write(line);
            }
            // Close the stream
            bufferedWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void readMessage() throws Exception {
        System.out.println("start with reading mood:");

        File file = new File("/home/sami/Desktop/file.txt");
        FileInputStream fileinputstream = new FileInputStream(file);

        try {
            int oneByte;
            while ((oneByte = fileinputstream.read()) != -1) {
                System.out.write(oneByte);
            }

            System.out.flush();
        } catch (Exception ex) {
            ex.getMessage();
        }

        fileinputstream.close();
    }

    @Override
    public void runUi(InputStream inp, OutputStream outp) throws Exception {
        File file = new File("/home/sami/Desktop/file.txt");
        FileReader filereader = new FileReader(file);
        BufferedReader br = new BufferedReader(filereader);

        // CmdLineUI obj = new CmdLineUI(consoleOutput);
        PrintStream ps = new PrintStream(outp);
        boolean again = true;

        while (again) {
            String[] commandMsg = readCommands();
            String command = commandMsg[0];

            switch (command) {
            case WRITE:
                writeMessage(br.readLine());
                again = true;
                break;

            case READ:
                readMessage();
                again = true;
                break;

            case EXIT:    
                again = false;
                break;

            default:
                ps.println(ERRORMESSAGE);
            }
        }

    }

}

我已经创建了这两个接口(interface) 这是第一个

public interface ChatUI {
    public void runUi (InputStream inp, OutputStream outp) throws Exception;
}

第二个界面

public interface Chat {
     //First Method 
    public void writeMessage(String message)throws Exception;

    // Second Method
    public void readMessage()throws Exception;
}

最佳答案

为您的 runUi() 方法编写测试,因为在所有其他方法中,您要么从控制台读取并对输入 String 进行标记,要么写入文件,要么读取从一个文件。所有这 3 个操作都已经过 Oracle 人员和/或 Java 语言开发人员的测试(如果您想了解更多信息,可以通过谷歌搜索这个过程)。

检查您的程序流程是否按预期工作并为它们编写测试并且不要理会您正在做的所有事情都是调用 Java API 然后除了返回(或写入某个地方)之外几乎什么都不做) 从它。

关于Java IO、输入流和输出流测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58747474/

相关文章:

Grails 集成测试 - getBean() 的空对象

java - 当高吞吐量(3GB/s)文件系统可用时,如何在 Java 中使用多线程读取文件

c++ - 用 C++ 将不同的结构写入文件?

java - 如何在Java中缓存httpclient对象?

java - 如何在 Java 中输入 π (pi)?

java - 从ice中获取列值:dataTable in the validator of another column

testing - 我如何使用 JustMock 来测试接口(interface)事件委托(delegate)

jquery - Jasmine 2.0 与 Jasmine-Jquery 的兼容性

c++ - 从 C++ 中的文本文件读取/写入

java - 来自子布局的 recyclerView 引用,包含在父布局中的 <include/>