1

之前问过如何使线程池中的单元测试失败。

@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 万无一失。

4

1 回答 1

0

我遇到了同样的问题,我的问题在这里: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 传递给在 Constructor 中调用 Runnable 类的类)

我在这样的测试方法中断言outSide:

@测试

public void assign_assign_release_release() throws ClientProtocolException, IOException, InterruptedException{

        assignUntilSuccess();
        releaseUntilSuccessAndExpect(1);
        Assert.assertTrue(resultWrapper.getResult());
    }

希望这会有所帮助。

于 2013-05-13T06:54:22.323 回答