java 如何单元测试客户端服务器代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3931246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HowTo Unit Test Client Server Code
提问by StaticBR
I'm currently writing a Java Client Server Application. So i want to implement two Libraries, one for the Client and one for the Server. The Client Server Communication has a very strict protocol, that I wan't to test with JUnit.
我目前正在编写 Java 客户端服务器应用程序。所以我想实现两个库,一个用于客户端,一个用于服务器。客户端服务器通信有一个非常严格的协议,我不想用 JUnit 测试。
As build tool im using Maven and a Husdon Server for continues Integration.
作为构建工具,我使用 Maven 和 Husdon 服务器进行持续集成。
Actually I do not have any good Idea how to test these Client / Server Libraries.
实际上,我对如何测试这些客户端/服务器库没有任何好主意。
I got following Approaches:
我得到了以下方法:
Just write a Dummy Client for testing the server and write a Dummy Server to test the Client. Disadvantages:Unfortunately this will result in many extra work. I could not be 100% sure that client and Server could work together, because I'm not sure that the Tests are completely identical.
Write an separate Test Project that Tests the Client and the Server together.
Disadvantages:The Unit Tests does not belong to the Project it self, so Hudson will not run them automatically. Everyone who changes anything at one of these Libraries, will have to run the Tests manually to ensure, everything is correct. Also i will not receive any Code Coverage Report.
只需编写一个虚拟客户端来测试服务器,并编写一个虚拟服务器来测试客户端。 缺点:不幸的是,这将导致许多额外的工作。我不能 100% 确定客户端和服务器可以一起工作,因为我不确定测试是否完全相同。
编写一个单独的测试项目,一起测试客户端和服务器。
缺点:单元测试不属于它自己的项目,因此 Hudson 不会自动运行它们。在这些库之一中更改任何内容的每个人都必须手动运行测试以确保一切正确。我也不会收到任何代码覆盖率报告。
Are there any better approaches to test codes like that? Maybe test a Maven Multi Module Project, or something like that.
有没有更好的方法来测试这样的代码?也许测试一个 Maven 多模块项目,或者类似的东西。
I hope any one got a good solution for that Issue.
我希望任何人都能为该问题找到一个好的解决方案。
Thanks.
谢谢。
采纳答案by StaticBR
So finally the resolution was to build a Multi Module Project, with a separate Test Module that includes the Server and the Client Module Works great in Husdon. And even better in the Eclipse IDE. Thanks @ Aaron for the hint
因此,最终的解决方案是构建一个多模块项目,其中包含一个单独的测试模块,其中包括在 Husdon 中运行良好的服务器和客户端模块。在 Eclipse IDE 中甚至更好。感谢@Aaron 的提示
回答by Aaron Digulla
Think of all your code as "transforms input to output": X -> [A] -> Y
将所有代码视为“将输入转换为输出”: X -> [A] -> Y
X
is the data that goes in, [A]
is the transformer, Y
is the output. In your case, you have this setup:
X
是输入的数据,[A]
是变压器,Y
是输出。在您的情况下,您有以下设置:
[Client] -> X -> [Server] -> Y -> [Client]
So the unit tests work like this:
所以单元测试是这样工作的:
You need a test that runs the client code to generate
X
. Verify that the code actually producesX
with an assert.X
should be afinal static String
in the code.Use the constant
X
in a second test to call the server code which transforms it intoY
(another constant).A third test makes sure that the client code can parse the input
Y
您需要一个运行客户端代码的测试来生成
X
.X
使用断言验证代码是否实际生成。X
应该是final static String
代码中的一个。X
在第二个测试中使用该常量来调用将其转换为Y
(另一个常量)的服务器代码。第三个测试确保客户端代码可以解析输入
Y
This way, you can keep the tests independent and still make sure that the important parts work: The interface betweenthe components.
通过这种方式,您可以保持测试的独立性,并且仍然确保重要的部分正常工作:组件之间的接口。
回答by Tim Clemons
My suggestion would be to use two levels of testing:
我的建议是使用两个级别的测试:
For your client/server project, include some mocking in your unit tests to ensure the object interfaces are working as expected.
Following the build, have a more extensive integration test run, with automation to install the compiled client and server on one or more test systems. Then you can ensure that all the particulars of the protocol are tested thoroughly. Have this integration test project triggered on each successful build of the client/server project. You can use JUnit for this and still receive the conventional report from Hudson.
对于您的客户端/服务器项目,在单元测试中包含一些模拟以确保对象接口按预期工作。
在构建之后,进行更广泛的集成测试运行,自动在一个或多个测试系统上安装已编译的客户端和服务器。然后您可以确保协议的所有细节都经过彻底测试。在每次成功构建客户端/服务器项目时触发此集成测试项目。您可以为此使用 JUnit,并且仍然可以接收来自 Hudson 的常规报告。
回答by ChaitanyaBhatt
The latest approach to solve this problem is by using Docker containers. Create a docker file containing a base image and all the necessary dependencies required for your client server application. Create a separate container for each node type of your distributed client-server system and test all the entry point server API/client interactions using TestNG or JUnit. The best part of this approach is that you are not mocking any service calls. In most cases you can orchestrate all the end-to-end client-server interactions.
解决这个问题的最新方法是使用 Docker 容器。创建一个包含基本映像和客户端服务器应用程序所需的所有必要依赖项的 docker 文件。为分布式客户端-服务器系统的每个节点类型创建一个单独的容器,并使用 TestNG 或 JUnit 测试所有入口点服务器 API/客户端交互。这种方法最好的部分是您不会嘲笑任何服务调用。在大多数情况下,您可以编排所有端到端的客户端-服务器交互。
There is a little bit of learning curve involved in this approach but Docker is becoming highly popular in the Dev community especially for solving this problem.
这种方法涉及一点学习曲线,但 Docker 在开发社区中变得非常流行,尤其是为了解决这个问题。
Here is an example of how you could use docker client api to pull docker images in your JUnit test: https://github.com/influxdb/influxdb-java/blob/master/src/test/java/org/influxdb/InfluxDBTest.java
以下是如何使用 docker 客户端 api 在 JUnit 测试中提取 docker 图像的示例:https: //github.com/influxdb/influxdb-java/blob/master/src/test/java/org/influxdb/InfluxDBTest .java
回答by Jayan
You may use any mock object framework to create mock objects - Try jmockit.
您可以使用任何模拟对象框架来创建模拟对象 - 试试 jmockit。