gpt4 book ai didi

java - Mockito Junit 测试 - “Wanted but not invoked; However there were other interactions with this mock” 错误

转载 作者:行者123 更新时间:2023-11-30 10:01:04 25 4
gpt4 key购买 nike

我必须在 Completable Future 中使用超时运行函数。只有当原始方法花费的时间超过给定的超时时间时,才应调用可运行函数。单位一直给=需要但未调用:但是,与此模拟正好有 3 次交互。

我想做的是,我正在尝试为方法执行添加超时 (getResponseWithTimeoutFunction(itemRequest)),如果该方法需要更多时间,则终止它并发布计数(以了解超时响应率)作为公制。

@Test
public void testTimeoutFunction() throws Exception {
Response response = getResponseForTest();
when(requestAdapter.transform(itemRequest)).thenReturn(Request);

when(dataProvider
.provide(any(Request.class)))
.thenAnswer((Answer<Response>) invocation -> {
Thread.sleep(1000000);
return response;
});

processor = spy(getProcessor());

when(itemRequest.getRequestContext()).thenReturn(itemRequestContext);
when(itemRequestContext.getMetadata()).thenReturn(requestContextMetadata);

List<Item> output = processor.getItemist(ITEM_ID, itemRequest);

assertTrue(output.isEmpty());
verify(processor, times(1)).processRequest(Request);
verify(processor, times(1)).responseTimedOutCount();
}

这是我正在测试的方法:

public class Process {

@VisibleForTesting
void responseTimedOutCount() {
//log metrics
}

private CompletableFuture<Response> getResponseAsync(final ScheduledExecutorService delayer,
final ItemRequest itemRequest) {
return timeoutWithTimeoutFunction(delayer, EXECUTION_TIMEOUT, TimeUnit.MILLISECONDS,
CompletableFuture.supplyAsync(() -> getResponseWithTimeoutFunction(itemRequest), executorService),
Response.emptyResponse(), () -> responseTimedOutCount());
}


private Response getResponseWithTimeoutFunction(final ItemRequest itemRequest) {
//do something and return response
}

public List<Item> getItemList(final String id, final ItemRequest itemRequest) throws Exception {

final ScheduledExecutorService delayer = Executors.newScheduledThreadPool(1);
Response response;
if(validateItemId(id){
try {
response = getResponseAsync(delayer, itemRequest).get();
} catch (final Throwable t) {
response = Response.emptyResponse();
} finally {
delayer.shutdown();
}
return transform(response, id).getItems();
} else {
return null;
}
}
}

并且超时函数使用=

public static <T> CompletableFuture<T> timeoutWithTimeoutFunction(final ScheduledExecutorService es,
final long timeout,
final TimeUnit unit,
final CompletableFuture<T> f,
final T defaultValue,
final Runnable r) {
final Runnable timeoutFunction = () -> {
boolean timedOut = f.complete(defaultValue);
if (timedOut && r != null) {
r.run();
}
};

es.schedule(timeoutFunction, timeout, unit);
return f;
}

Junit 异常:

   Wanted but not invoked: process.responseTimedOutCount(); -> at processTest.testTimeoutFunction(processTest.java:377) 
However, there were exactly 3 interactions with this mock:
process.getItemList( ITEM_ID, itemRequest ); -> at processTest.testTimeoutFunction(processTest.java:373)
process.validateItemId( ITEM_ID ); -> at process.getItemList(process.java:133)
process.processRequest( request ); -> at process.getResponseWithTimeoutFunction(process.java:170)

最佳答案

要测试超时,您可能需要模拟要测试超时的调用。相对于测试的持续时间,它应该永远持续下去。

when(dataProvider
.provide(any(Request.class)))
.thenAnswer((Answer<Response>) invocation -> {
Thread.sleep(FOREVER);
return response;
});

验证应该有一个线程处理超时。当超时很长时,您可能应该确保它是可配置的以允许快速测试。像 verify(mock, ti​​meout(LONGER_THAN_REAL_TIMEOUT)).someCall()

这样的东西

确保对总测试持续时间设置超时,以确保当前或 future 的失败不会减慢您的构建速度。

关于java - Mockito Junit 测试 - “Wanted but not invoked; However there were other interactions with this mock” 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57649428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com