如何通过Junit和timeout获得成功? [英] How to get a success with Junit and timeout?

查看:122
本文介绍了如何通过Junit和timeout获得成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Test (expected=TimeoutException.class,timeout=1000)
public void fineForFiveSeconds() {
    foo.doforever();
    fail("This line should never reached");
}

这是我的测试代码. 我只想运行doforever()一段时间,然后使测试成功.

This is my test code. All I want is to run doforever() for some time period then make the test succeed.

推荐答案

尝试一下:

在线程中执行逻辑,休眠并检查线程是否仍在运行.

Execute the logic in a thread, sleep and check if the thread is still alive.

@Test
public void fineForFiveSeconds() throws InterruptedException {
  Thread thread = new Thread() {
    @Override
    public void run() {
      foo.doforever();
    }
  };

  thread.start();

  //Let the current thread sleep (not the created thread!)
  Thread.sleep(5000);

  assertTrue(thread.isAlive());
}

这篇关于如何通过Junit和timeout获得成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆