本文介绍了我在代码中不拥有的回调导致TestNG单元测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前曾问过如何使线程池中的单元测试失败.

I previously asked how to fail a unit test from a thread pool.

@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并获取.但是在我的实际设置中,我既不拥有线程池也不拥有Submit调用-这是一个最终源自MINA的回调.

The solution I accepted was to block on the returned Future and get. But in my actual setting I own neither the threadpool nor the submit call - it's a callback that ultimately originates from MINA.

关于最好的主意,我正在弄乱全局默认异常处理程序,但看起来很笨拙.

About the best idea I have is messing around with the global default exception handler but seems very kludgy.

如果有必要,请确保确保成功.

Maven surefire if it matters.

推荐答案

我遇到相同的问题,而我的问题在这里:

I meet the same question, and my question is here: TestNG Make assert in multithread way (not run in multithread)

这是我的解决方案:

由于Java通过引用传递方法的参数,所以我写了一个ResultWrapper类

As java pass method's parameter by reference, so I wrote a ResultWrapper class

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);

Instead of making any assert in public void run() I put the result into ResultWrapper: resultWrapper.setResult(actual == expectedAssert);

(将ResultWrapper传递给在构造函数中调用Runnable类的类)

(the ResultWrapper is pass to the class which call the Runnable class in Constructor)

然后在这样的测试方法中声明assertSide:

And I make assert outSide in test method like this:

@Test

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

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

希望这会有所帮助.

这篇关于我在代码中不拥有的回调导致TestNG单元测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!