简体   繁体   English

Java Chat Server(Socket)-如何编写单元测试?

[英]Java Chat Server (Socket) - how to write unit test?

I have written a Java Chat Server program. 我已经编写了一个Java Chat Server程序。 This is a simple standalone program for Server. 这是服务器的简单独立程序。 I have to run this then run Client to get Chat working. 我必须先运行此程序,然后运行Client才能使Chat正常工作。 What are some possible Unit Test scenarios for the server program? 服务器程序可能有哪些单元测试方案? Can anyone show me some example of unit test based on this code? 谁能给我展示一些基于此代码的单元测试示例? I have never written a unit test code before and I can't really think of what needs to be tested here.. I think testing Connection can be one but what else? 我以前从未编写过单元测试代码,而且我真的想不到这里需要测试什么。.我认为测试Connection可以是一个,但是还有什么呢? (and how to?) (以及如何?)

public class SimpleChatServer {

    static final Logger logger = LogManager.getLogger(SimpleChatServer.class);      
    ArrayList<PrintWriter> clientOutputStreams;
    private BufferedReader reader;
    private Socket sock;
    private ServerSocket serverSock;

    public class ClientHandler implements Runnable{

        public ClientHandler(Socket clientSocket){  // Socket Connection
            try {
                sock = clientSocket;
                InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
                reader = new BufferedReader(isReader);
            } catch(Exception ex) {
                logger.trace(ex);
            }
        }

            public void run() {
                String message;
                try {
                    while ((message = reader.readLine()) != null) {
                        System.out.println("read " + message);
                        tellEveryone(message);
                    }
                } catch(Exception ex) {
                    logger.trace(ex);
                }
            } //close run

    } //close ClientHandler

        public static void main (String[] args) throws Exception
        {
            new SimpleChatServer().listen();
        }

        @SuppressWarnings("resource")
        public void listen()
        {
            clientOutputStreams = new ArrayList<PrintWriter>();
            try {

                ServerSocket serverSock = new ServerSocket(8000); //port number 8000 was used
                while(true) {
                    Socket clientSocket = serverSock.accept();
                    PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
                    clientOutputStreams.add(writer);

                    Thread t = new Thread(new ClientHandler(clientSocket));
                    t.start();
                }
            } catch (Exception ex) {
                logger.trace("Server Error", ex);
            } finally {
                try
                {
                    serverSock.close();
                }
                catch(Exception e){}
            }

        } // close go

        public void tellEveryone(String message)
        {
            Iterator<PrintWriter> it = clientOutputStreams.iterator();
            while(it.hasNext()) {
                try {
                    PrintWriter writer = (PrintWriter) it.next();
                    writer.println(message);
                    writer.flush();
                } catch (Exception ex) {
                    logger.trace(ex);
                }
            } // end while
        } // close tellEveryone

}

I was going to crib an answer from Pragmatic Unit Testing , but suggest you just find a copy. 我本来想从实用单元测试中获得答案,但建议您只找一份。 At the very least you should consider whether results are right, whether your boundary conditions are correct, and if you can force error conditions. 至少应考虑结果是否正确,边界条件是否正确以及是否可以施加错误条件。

Testing results often means making sure combinations of input get the expected results. 测试结果通常意味着确保输入组合得到预期结果。 Boundaries are reflected in the related "0, 1, many" rule, where you do silly stuff to see if your code has implicit boundaries that can be reached with bad, null or unexpected values. 边界反映在相关的“ 0,1,many”规则中,您在其中做一些愚蠢的工作,以查看您的代码是否具有可以使用错误,空值或意外值到达的隐式边界。

For example, what happens if you pass huge Strings to your methods that take them? 例如,如果将巨大的字符串传递给采用它们的方法会怎样? What about strings with weird Unicode chars in them? 带有奇怪的Unicode字符的字符串呢? No line breaks? 没有换行符?

Forcing error conditions means making sure things degrade gracefully and/or throw under the expected situation. 强制错误条件意味着确保在预期情况下性能正常下降和/或抛出。

Think about your code as a brittle little appliance and then pretend a poo-flinging monkey, a 14-yr old hacker and your non-hacker grandmother (I know some exist) are all taking turns on it. 将您的代码想像成一个易碎的小工具,然后假装一只便便大便的猴子,一个14岁的黑客和您的非黑客祖母(我知道有些人存在)都在轮流使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM