简体   繁体   English

如何使用JUnit测试两个都具有Java主要方法的类(客户端和服务器)?

[英]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.

相关问题 我如何使用JUnit在一个Java类中对测试方法进行单元化而又没有在模块中完成其他类? - How do I use JUnit to unit test methods in one Java class, without having completed other classes in the module? 如何使用JUnit测试ArrayList的getter和setter方法? - How do I use JUnit to test the getter and setter methods of an ArrayList? 如何测试在构造函数中也调用该方法的类的方法? (JUnit的) - How do I test methods of classes in which the method is also called in the constructor? (JUnit) 有什么方法可以测试使用JUnit访问ContentResolver的类? 还是我必须使用Robolectric? - Is there any way to test classes that access ContentResolver with JUnit? Or do I have to use Robolectric? 我在同一个程序包中有两个Java文件-如何同时运行两个文件的测试? - I have two java files in the same package- How do I run a test of both files at the same time? 如何使用模拟来测试Java Junit中带有参数的方法? - How to use mocking to test the methods with parameters in java Junit? 如何使用 Java 中的 junit 为 void 方法编写测试,尤其是它们不返回值? - how can I write a test for void methods with junit in Java especially they do not return a value? 在 Java 中如何使用主类中子类的方法? - How do you use methods from sub classes in the main class in Java? 我必须使用哪些类和方法从 C++ 调用 Java? - What classes and methods do I have to use to call Java from C++? Java:从 jUnit 调用主要实现的接口方法 - 测试 - Java: Calling in main implemented Interface Methods from a jUnit - Test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM