Java 聊天应用程序的单元测试
我用 Java 开发了一个基本的聊天应用程序。它由一个服务器和多个客户端组成。服务器持续监视传入的消息并将其广播给所有客户端。客户端由 Swing GUI 组成,其中包含一个文本区域(用于服务器和其他客户端发送的消息)、一个文本字段(用于发送文本消息)和一个按钮 (SEND)。客户端还持续监视来自其他客户端(通过服务器)的传入消息。这是通过线程和事件侦听器实现的,并且应用程序按预期工作。
但是,我该如何对我的聊天应用程序进行单元测试呢?由于这些方法涉及与服务器建立连接以及从服务器发送/接收消息,因此我不确定这些方法是否应该进行单元测试。根据我的理解,单元测试不应该针对连接到数据库或网络等任务进行。
我能想到的几个测试用例是:
1) 文本字段的最大限制
2) 客户端可以连接到服务器
3)服务器可以连接到客户端
4) 客户端可以发送消息
5) 客户端可以接收消息
6) 服务器可以发送消息
7) 服务器可以接收消息
8)服务器可以接受来自多个客户端的连接
但是,由于上述大多数方法都涉及某种网络通信,因此我无法执行单元测试。我应该如何对我的聊天应用程序进行单元测试?
I have developed a basic Chat application in Java. It consists of a server and multiple client. The server continually monitors for incoming messages and broadcasts them to all the clients. The client is made up of a Swing GUI with a text area (for messages sent by the server and other clients), a text field (to send Text messages) and a button (SEND). The client also continually monitors for incoming messages from other clients (via the Server). This is achieved with Threads and Event Listeners and the application works as expected.
But, how do I go about unit testing my chat application? As the methods involve establishing a connection with the server and sending/receiving messages from the server, I am not sure if these methods should be unit tested. As per my understanding, Unit Testing shouldn't be done for tasks like connecting to a database or network.
The few test cases that I could come up with are:
1) The max limit of the text field
2) Client can connect to the Server
3) Server can connect to the Client
4) Client can send message
5) Client can receive message
6) Server can send message
7) Server can receive message
8) Server can accept connections from multiple clients
But, since most of the above methods involve some kind of network communication, I cannot perform unit testing. How should I go about unit testing my chat application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该单独测试服务器和客户端。
做到这一点的方法是使用 模拟对象 来模拟服务器(用于测试客户端)或客户端(用于测试服务器)。
模拟服务器将具有与真实服务器相同的方法,但您可以决定它们返回什么,即模拟连接错误、超时等。因为它是模拟,所以您可以完全控制功能,并且不需要不必担心实际的连接错误。
对于 Java,请查看 Mockito 模拟框架。
You should test the server and client in isolation.
The way to do this is to use mock objects to mock either the server (for testing the client) or the client (for testing the server).
A mock server would have the same methods as the real server, but you can decide what they return, i.e. simulate a connection error, a timeout, etc. Because it is a mock, you have full control over the functioning and you don't have to worry about actual connection errors.
For Java, look at the Mockito mocking framework.
单元测试应重点关注您构建的每个类的公共 API。然而,处理 Swing 时事情会变得有点棘手。考虑使用 swingUnit 进行 Swing 组件的单元测试。
Unit tests should be focused on exercising public APIs of each class you have built. However, things get a little tricky when dealing with Swing. Consider swingUnit for unit testing Swing components.
美丽测试的第 7 章介绍了测试 XMPP 聊天客户端。我建议阅读本章。结论是说明性的,可以为您的聊天应用程序提供一些指导:
Chapter 7 of Beautiful Testing describes testing an XMPP chat client. I recommend reading the chapter. The conclusion is illustrative and may provide some pointers for your chat application: