套接字超时的junit测试用例

发布于 2024-12-02 07:12:40 字数 318 浏览 0 评论 0原文

我有一个支持一些出站 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七颜 2024-12-09 07:12:40

如果您希望为代码编写真正的隔离单元测试,那么我同意您的测试不合适。如果您希望编写更多集成测试,那么您的方法就很好。

对于我的隔离单元,我创建了它们,以便它们只测试被测类。我使用 EasyMock 来模拟所有协作类。所以在这种情况下,我将为 httpClient 创建一个模拟。我将设置一个测试,其中模拟抛出异常,然后断言被测试的类按预期处理异常。下面的代码片段可能会让您了解我的意思。

private DefaultHttpClient httpClient;
private ClassUnderTest classUnderTest;

@Before
public void setUpTest() {
     httpClient = createMock(DefaultHttpClient.class);

@Test
public void performARequestThatThrowsAnIOException() {
     expect(httpClient.execute(post)).andThrow(iOException);

     replayAll();

     try {
         classUnderTest.executeMethodUnderTest();
         fail("This test should throw an IOException.");
     } catch (IOException e){
         verifyAll();
     }

上面假设被测类中的方法抛出异常。如果捕获到异常,那么您会以不同的方式编写上面的内容。

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.

private DefaultHttpClient httpClient;
private ClassUnderTest classUnderTest;

@Before
public void setUpTest() {
     httpClient = createMock(DefaultHttpClient.class);

@Test
public void performARequestThatThrowsAnIOException() {
     expect(httpClient.execute(post)).andThrow(iOException);

     replayAll();

     try {
         classUnderTest.executeMethodUnderTest();
         fail("This test should throw an IOException.");
     } catch (IOException e){
         verifyAll();
     }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文