我previously asked如何使线程池中的单元测试失败。
@Test
public void foo() throws Exception {
ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(1);
stpe.submit(new Runnable() {
@Override
public void run() {
// does not make this unit test fail :(
Assert.AssertEquals(1, 2);
}
});
}
我接受的解决方案是阻止返回的 Future
并获取。但在我的实际设置中,我既不拥有线程池,也不拥有提交调用——它是最终源自 MINA 的回调。
关于我最好的想法是搞乱全局默认异常处理程序,但看起来很笨拙。
如果重要的话,Maven 一定会成功。
最佳答案
我遇到了同样的问题,我的问题在这里:TestNG Make assert in multithread way (not run in multithread)
这是我的解决方案:
由于java通过引用传递方法的参数,所以我写了一个ResultWrapper类
public class ResultWrapper {
boolean result;
public boolean getResult()
{
return result;
}
public void setResult(boolean result)
{
this.result = result;
}
}
我没有在 public void run()
中进行任何断言,而是将结果放入 ResultWrapper 中: resultWrapper.setResult(actual == ExpectedAssert);
(ResultWrapper被传递给构造函数中调用Runnable类的类)
我在测试方法中进行断言outSide,如下所示:
@测试
public void assign_assign_release_release() throws ClientProtocolException, IOException, InterruptedException{
assignUntilSuccess();
releaseUntilSuccessAndExpect(1);
Assert.assertTrue(resultWrapper.getResult());
}
希望这会有所帮助。
关于java - 代码中不属于我的回调导致 TestNG 单元测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15170055/