套接字超时的junit测试用例
我有一个支持一些出站 Web 服务的应用程序。我是 使用 HttpClient,我需要添加服务超时,因为其中之一 我访问的外部 URL 需要很长时间才能响应(有时)。 我正在尝试添加一些 junit 测试用例以确保我的超时正常工作 适当地。我有这样的东西,其中指定的超时值是 比配置的套接字超时多一点——
@Test (timeout=6000)
public void testTimeOut() {
notifier.performGetonUrl(getTestUrl());
}
我觉得它不是一个好的超时。我可以使用更好的测试吗?
I have an application that supports a few outbound web services. I am
using HttpClient and I need to add a service time out because one of the
external URLs that I am accessing takes a long time to respond (at times).
I am trying to add a few junit test cases to make sure my timeout is working
properly. I have something like this where the specified timeout value is
little more than the configured socket timeout -
@Test (timeout=6000)
public void testTimeOut() {
notifier.performGetonUrl(getTestUrl());
}
I don't feel its a good one. Are there better tests I could use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望为代码编写真正的隔离单元测试,那么我同意您的测试不合适。如果您希望编写更多集成测试,那么您的方法就很好。
对于我的隔离单元,我创建了它们,以便它们只测试被测类。我使用 EasyMock 来模拟所有协作类。所以在这种情况下,我将为 httpClient 创建一个模拟。我将设置一个测试,其中模拟抛出异常,然后断言被测试的类按预期处理异常。下面的代码片段可能会让您了解我的意思。
上面假设被测类中的方法抛出异常。如果捕获到异常,那么您会以不同的方式编写上面的内容。
If you are looking to write a true isolation unit test of your code then I agree that your test isn't appropriate. If you are looking to write more of an integration test then your approach is fine.
For my isolation junits I create them so that they only test the class under test. I use EasyMock to mock out all collaborating classes. So in this case I would create a mock for httpClient. I would set up a test where the mock throws the exception, and then assert that the class under test handles the exception as expected. The code snippets below may give you an idea of what I mean.
The above assumes that the method in the class under test throws the exception. If the exception is caught then you would write the above differently.