简体   繁体   中英

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. 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.

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.

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()。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM