如何在Java服务器程序上进行单元测试和集成测试

时间:2020-10-01 15:02:03

标签: java unit-testing testing integration-testing

我有以下2个文件:

--- Server.java ---

package chatrooms.model.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;


/**
 * Server in the client-server model
 */
public class Server {
    public static int SERVER_PORT_NR = 6000; 
    private final ArrayList<Integer> activePorts = new ArrayList<>();
    private boolean constructed;

    public Server() {
        constructed = true;
    }

    /**
     * Start the server and perform the following steps:
     * 1. Wait for a connection from a chatroom/bot
     * 2. Create a new Socket for the room/bot to connect to
     * 3. Run a new instance of ThreadHandler on a new thread for this socket
     */
    public void start() {
        try (ServerSocket serverSocket = new ServerSocket(SERVER_PORT_NR)) {

            System.out.println("Server waiting for chatroom connections...");

            while (true) {
                if (!activePorts.isEmpty()) System.out.println(activePorts);
                Socket socket = serverSocket.accept();

                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                String typeOfConnection = in.readLine();

                if (typeOfConnection.equals("Chat")) {
                    String roomName = in.readLine();
                    System.out.println("Chatroom \"" + roomName + "\" is requesting a port number");
                    int portNr = Integer.parseInt(String.valueOf(in.readLine()));
                    activePorts.add(portNr);
                    System.out.println("... " + roomName + " has activated port number: " + portNr);
                    System.out.println("Current open chatrooms: " + activePorts.size());
                    socket.close();
                    in.close();
                } else if (typeOfConnection.equals("BotMan")) {

                    System.out.println("BotManager is connecting to the main server... ");
                    ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
                    os.writeObject(activePorts);
                    System.out.println("... BotManager Successfully connected");

                } else throw new ClassNotFoundException(); // There must be a better exception ??
            }

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO exception");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Unknown connection type!");
        }
    }

    public boolean isConstructed() {
        return constructed;
    }
}

--- ServerMain.java ---

package chatrooms.model.server;

public class ServerMain {
    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }
}

我的问题是,我应该如何对这些文件进行单元测试和集成测试?就像,我应该在start()方法中到底测试什么?如何进行测试?

到目前为止,我的测试课程如下:

package chatrooms.model.server;


import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class TestServerMain {

    @Test
    public void testOne(){
        Server server = new Server();
        assertTrue(server.isConstructed(), " Server is not constructed");
        server.start();



    }
}

谢谢您的帮助!我真的不知道该从哪里开始...

0 个答案:

没有答案