Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 此处检测到mockito的间歇性未完成短截线故障?_Java_Unit Testing_Junit_Mockito_Testng - Fatal编程技术网

Java 此处检测到mockito的间歇性未完成短截线故障?

Java 此处检测到mockito的间歇性未完成短截线故障?,java,unit-testing,junit,mockito,testng,Java,Unit Testing,Junit,Mockito,Testng,我有一个单元测试,它只在某些时候失败,并出现以下错误: Unfinished stubbing detected here: -> at com.example.testHandlePublish(MyTestClass.java:78) E.g. thenReturn() may be missing. Examples of correct stubbing: when(mock.isOk()).thenReturn(true); when(mock.isOk()).

我有一个单元测试,它只在某些时候失败,并出现以下错误:

Unfinished stubbing detected here:
-> at com.example.testHandlePublish(MyTestClass.java:78)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, which is not supported
 3. you are stubbing the behaviour of another mock inside before 'thenReturn' instruction is completed
测试如下。关于这个特定错误,这里有各种各样的问题,主要原因似乎是嵌套模拟(上面的提示3):

但我认为情况并非如此,因为考试大部分时间都通过了

@Test
public void testHandlePublish()
    throws IOException, InterruptedException
{
    BlockingQueue<Event<QueueConsumer>> consumerEvents = new LinkedBlockingQueue<>();
    BlockingQueue<Event<WorkerPublisher>> publisherEvents = new LinkedBlockingQueue<>();
    Channel channel = Mockito.mock(Channel.class);
    WorkerConfirmListener listener = Mockito.mock(WorkerConfirmListener.class);
    WorkerPublisher impl = new WorkerPublisherImpl(channel, metrics, consumerEvents, listener);
    EventPoller<WorkerPublisher> publisher = new EventPoller<>(2, publisherEvents, impl);
    Thread t = new Thread(publisher);
    t.start();
    publisherEvents.add(new WorkerPublishQueueEvent(data, "testQueue", taskInformation));
    CountDownLatch latch = new CountDownLatch(1);
    Answer<Void> a = invocationOnMock -> {
        latch.countDown();
        return null;
    };
    Mockito.doAnswer(a).when(channel).basicPublish(Mockito.any(), Mockito.eq("testQueue"), Mockito.any(), Mockito.eq(data));
    latch.await(5000, TimeUnit.MILLISECONDS);
    publisher.shutdown();
    Assert.assertEquals(0, publisherEvents.size());
    Assert.assertEquals(0, consumerEvents.size());
}

在完成模拟设置之前,您是否确实需要开始运行测试(通过启动publisher线程)?@tgdavie可能不需要,我可以将
Mockito.doAnswer
移动到
t.start()
上方。您是否怀疑这可能是该测试随机失败的原因?这是我看到的mockito错误消息的原因吗?我不确定,但这可能会使测试更加一致。在完成mock设置之前,您确实需要开始运行测试(通过启动发布者线程)吗?@tgdavie可能不需要,我可以将
mockito.doAnswer
移到
t.start()上方
。您是否怀疑这可能是该测试随机失败的原因?这能解释我看到的mockito错误消息吗?我不确定,但它可能会使测试更加一致。
Mockito.doAnswer(a).when(channel).basicPublish(Mockito.any(), Mockito.eq("testQueue"), Mockito.any(), Mockito.eq(data));