[英]How do I use JUnit to test two classes (a client and a server) that both have main methods in Java?
I have a client class that uses a main function and a server class that uses a main function. 我有一个使用主函数的客户端类和一个使用主函数的服务器类。 Junit won't let me use classes with a main function.
Junit不允许我使用具有主要功能的类。 I can easily create the two classes without the main functions, but will they both be able to run side by side???
我可以轻松地创建两个没有主要功能的类,但是它们都可以并排运行吗???
Generally speaking, unit testing should be used for testing classes and their methods, not entire programs. 一般来说,单元测试应该用于测试类及其方法,而不是整个程序。 main() inherently suggests that you dealing with an entire program so you probably don't want to test that.
main()本质上建议您处理整个程序,因此您可能不想测试它。
A common pattern is to have a simple main() in your class (or some sort of separate MyClassDriver class) that instantiates and invokes the main methods of your class. 一种常见的模式是在类(或某种单独的MyClassDriver类)中具有一个简单的main(),该类实例化并调用类的主要方法。
If you have a server, your unit tests should usually be scoped to test the response of the server to a particular request or to a sequence of requests. 如果您有服务器,则通常应将单元测试的范围确定为测试服务器对特定请求或一系列请求的响应。
If you have a client, you usually want to test the behaviour of the client in terms of how it sends requests to the server in response to some input, and how it processes the responses from the server. 如果您有客户端,则通常需要根据客户端如何响应某些输入将请求发送到服务器以及客户端如何处理服务器的响应来测试客户端的行为。
如果您将类称为“客户端和服务器”,请依次调用Server.main()和Client.main()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.