用Java对客户机-服务器应用程序进行单元测试
我正在开发一个简单的客户机-服务器应用程序,我应该在单独的线程中初始化我的客户机和服务器对象。服务器包含我所在大学某些特定系的所有课程的数据库,GET请求获取所请求系的所有课程,并将其发送回客户端。虽然我能够用一种主要方法完美地测试所有东西,但单元测试根本不起作用。下面是我的单元测试代码:用Java对客户机-服务器应用程序进行单元测试,java,multithreading,sockets,junit,Java,Multithreading,Sockets,Junit,我正在开发一个简单的客户机-服务器应用程序,我应该在单独的线程中初始化我的客户机和服务器对象。服务器包含我所在大学某些特定系的所有课程的数据库,GET请求获取所请求系的所有课程,并将其发送回客户端。虽然我能够用一种主要方法完美地测试所有东西,但单元测试根本不起作用。下面是我的单元测试代码: import client.Client; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; im
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()测试的以下输出:
我一直在寻找让单元测试与线程一起工作的最佳方法,但我似乎没有找到答案。任何帮助都将不胜感激。通常,单元测试应该只测试单个组件。“单位” 你不需要做一些集成测试。因此,在调用@Test之前,您必须确保@Test所需的每个组件都已运行 我不确定JUnit是否是最好的框架。但是您可以在@Before方法中添加一个check,该方法将使主线程休眠,直到服务器运行为止 例如:
@Before
public void startServerTest(){
Server = new Server();
Thread serverThread = new Thread(() -> {
server.init(file);
});
serverThread.start();
while (!server.isRunning() {
Thread.sleep(1000);
}
}
首先,正如我的同事们所说的,这远远不是单元测试,而是集成或端到端测试 您的测试的问题可能是,某些线程的性能优于创建随机失败测试的其他线程,因此您需要一些选项来同步它们 我可以说,使用JUnit进行此类测试是完全可能的,但要进行此类测试,需要将一些关键概念付诸实践,包括多线程、可复制(而不是不稳定)和任何有用的功能 没有看到
Server
类的实现,这是相当困难的,但我试图总结一下这些提示,希望能让您走上正确的道路
第一名:
在同一线程内(最好在主线程中)创建服务器类和所有其他依赖项
重要的是,除了分配实例变量(如this.someVar=someVar
)之外,构造函数不能做任何“工作”。它背后的概念被称为,使你的生活变得更简单,特别是如果你使用一个图书馆
秒:
你的应用程序需要钩子,测试可以插入钩子来拦截你的逻辑,你可以让线程停止并等待其他必要的事件发生,以使测试正常工作。插入到何处在很大程度上取决于您的应用程序。很可能需要为测试中的某些类指定不同的实现,从而以不同的方式连接应用程序的依赖关系图
为了实现同步,您最好的朋友是测试类中的助手
@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());
...
}
这个例子只是简单地说明了什么是可能的,以及如何利用这个概念来维持高水平的自动化测试,但它应该展示了背后的想法。如果没有看到这些类的实现,很难说——但是在每次测试后停止服务器可能会有所帮助。serverThread是局部变量,最有可能在it事件开始执行“新服务器(文件)”之前被处理掉:这远远不是一个单元测试。您正在测试整个系统,而不仅仅是一个代码单元。现在,问题可能是您正在尝试在启动服务器线程后立即连接到服务器。此时,您无法保证服务器线程正在其套接字上执行和侦听,因为测试线程和服务器线程同时运行,并且thread.start()不会等待服务器实际启动。我对“new server(file);”有问题,这是阻塞调用吗?如何关闭服务器?serverThread
应声明为数据成员,并在每次测试之前/之后(@after
)启动/停止。
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
}
@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());
...
}