监视线程对象后,thread.start(),thread.join()不起作用。

时间:2020-04-14 03:26:29

标签: mockito spy

我找不到任何使用Mockito专门提到线程对象间谍的文档。但是我使用以下代码(使用了嘲讽1.8.5)进行了测试:

@Test
public void testThread() throws InterruptedException
{
    Thread threada = spy( new Thread( new Runnable()

    {
        @Override
        public void run()
        {
            System.out.println( "on thread" );
        }
    } ) );
    threada.start();
    //threada.join();
    System.out.println( "finished test" );
}

“ on thread”未打印出来,如果我将threada.join()放入,则会出现异常:

Exception: java.lang.StackOverflowError thrown from the UncaughtExceptionHandler in thread "Thread-1"
finished test

如果我删除“ spy()”,则会得到两个打印结果(“无线程”和“完成的测试”),并且也不例外。为什么我不能监视线程对象?

1 个答案:

答案 0 :(得分:1)

请使用Mockito的当前版本(2.X而不是1.8.5) 我检查了您的代码,问题在1.8.5上重现,并在2.X上消失

2.X进行了重大更改-将模拟生成器引擎从CGLIB更改为ByteBuddy。

相关问题