使用Java测试客户端 - 服务器应用程序的单元

时间:2017-10-02 07:39:36

标签: java multithreading sockets junit

我正在研究一个简单的客户端 - 服务器应用程序,我应该在不同的线程中初始化我的客户端和服务器对象。服务器包含我所在大学某些特定部门的所有课程的数据库,GET请求获取所请求部门的所有课程并将其发送回客户端。虽然我能够在主方法中完美地测试一切,但单元测试却无法正常工作。下面是我的单元测试代码:

import client.Client;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import server.Server;


public class ClientServerTest {
    private static String file = "";

    @BeforeClass
    public static void setFile(){
        file = System.getProperty("file");
    }

    @Before
    public void startServerTest(){
        Thread serverThread = new Thread(() -> {
            new Server(file);
        });
        serverThread.start();
    }

   @Test
   public void getTest(){
        Client client = new Client();
        Assert.assertTrue(client.submitRequest("GET COM"));
        Assert.assertTrue(client.submitRequest("GET MAT"));
        Assert.assertTrue(client.submitRequest("GET BIO"))
    }
}

当我运行测试时,服务器测试通过,我获得了getTest()测试的以下输出:

java.net.ConnectException: Connection refused: connect

我一直在寻找最佳方法让单元测试与线程一起工作,但我似乎无法弄明白。任何帮助都将非常感激。

2 个答案:

答案 0 :(得分:0)

通常,UnitTest只应测试单个组件。 "单位"。

你想做某种集成测试。因此,在调用@Test之前,必须确保@Test所需的每个组件都在运行。

我不确定JUnit是否是最好的框架。但是你可以在@Before方法中添加一个检查,它将在主服务器运行之前休眠主线程。

例如:

    @Before
    public void startServerTest(){
    Server = new Server();
            Thread serverThread = new Thread(() -> {
                server.init(file);
            });
            serverThread.start();
    while (!server.isRunning() {
        Thread.sleep(1000);
    }
}

答案 1 :(得分:0)

首先,正如我的同事所说,这远远不是单元测试,而是集成或端到端测试。

您的测试问题可能是某些线程优于其他线程创建随机失败的测试,因此您需要一些选项来同步它们。

我可以说,使用JUnit进行这种测试是绝对可能的,但是有一些关键概念需要付诸实践才能进行这样的测试,涉及多个线程,可重现(不易碎)和任何有用的。

如果没有看到Server课程的实施,那就非常困难,但我总结了一些希望能让你走上正轨的提示。

<强>首先

在同一个线程内创建您的Server类和所有其他依赖项(最好在主线程中)。

public static void main(String[] args) {
   Foo otherInstance = new Foo(...);
   Bar anotherInstance = new BarImpl(otherInstance,...);
   Server server = new Server(otherInstance, anotherInstance);
   server.start(); // blocking until app is stopped in a graceful way and where real application logic starts
   // not more to do here
}

重要的是,施工人员不得做任何工作&#34;除了分配像this.someVar = someVar这样的实例变量。其背后的概念称为dependency injection,使您的生活更加轻松,特别是如果您使用图书馆。

<强>第二

您的应用程序需要挂钩,测试可以插入以拦截您的逻辑,您可以在其中停止线程并等待使测试工作所必需的其他事件。插入的位置在很大程度上取决于您的应用。最有可能需要为测试中的某些类指定不同的实现,从而对应用程序的依赖图进行不同的连接。

要完成同步,CountDownLatch之类的辅助工具是您在测试类中最好的朋友。

@Test
public void someTest() {
    // or move the instantiation logic to the @Before setup method
    final CountDownLatch isConnected = new CountDownLatch(1);
    Foo otherInstance = new Foo(...);
    Bar anotherInstance = new BarTestImpl(otherInstance,...);
    // here we plug into the connected event
    anotherInstance.setConnectionListener(() -> { isConnected.countDown(); }
    Server server = new Server(otherInstance, anotherInstance);
    server.startAsync(); // not blocking
    ...
    // test logic starting, do some assertions or other stuff
    ...
    // block until connected event is received
    isConnected.await(1, TimeUnit.SECONDS);
    // do some assertions
    assertTrue(server.isConnected());
    ...
}

这个例子正在探讨可能的表面,以及如何利用这个概念来维持高水平的自动化测试,但它应该显示出背后的想法。