开发者

jUnit - is it possible to run tests NOT concurrently?

I'm testing a 开发者_如何转开发small proxy server program and my problem is that tests initiate the proxy to a certain port. I can use a different port every time and I get have two servers listening in on the same port. How do I make jUnit (in Eclipse) run tests one after another and not simultaneously?


JUnit only runs one test at a time, unless you do something special to make it do otherwise. What is likely happening in your case is that the proxy started for one test is not stopping, or not stopping in time, before the second test runs.

EDIT: In response to your comment, there are a couple of ways to handle this. One is to put the test in the same package as the class, so that you can use a package private method (which means it has no declaration of public private or anything else) that closes the socket and call that method from the test, so it is closed, but it is not part of the public API.

Another option is to pass in the constructor a socket generator. In production you might not pass one in, and rather just have an internal implementation, but under test you pass one that is created by your test code, and your test code closes it at the end of the test.


I've seen this kind of problem with sockets before. On different OS closing a port might take more time than the VM says. I would recommend that you make the tests sleep and/or timeout until they can open the server port. It is a lame solution, I know.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜