tro*_*erg 2 java unit-testing mocking mockito
这是我试图嘲笑的方法:
@VisibleForTesting
public List<Row> processRows2(CompletableFuture future) {
List<Row> rows2 = new ArrayList<>();
try {
DefaultAsyncResultSet beep = (DefaultAsyncResultSet) future.get();
for (Row b : beep.currentPage()) {
rows2.add(b);
}
}
catch (ExecutionException | InterruptedException e) {
LOGGER.error(e);
LOGGER.error(e.getStackTrace());
throw new RuntimeException(e.getMessage() + " - Check thread pool resources are enough, may be too many queries in queue");
}
return rows2;
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试用它来测试它时(目前只是想让它一直运行到成功或失败):
@Test
public void processRows2test() {
FeatureDaoImpl gar = new FeatureDaoImpl(connection);
CompletableFuture L = new CompletableFuture();
gar.processRows2(L);
}
Run Code Online (Sandbox Code Playgroud)
它无休无止地挂着。我的猜测是 future.get() 是挂在哪里;我不知道。但我不知道如何嘲笑它。我试过这个:
@Mock
private CompletableFuture mockFutures;
@Before
public void setUp() {
try {
Mockito.when(mockFutures.get()).thenReturn((AsyncResultSet) mockResultSetFuture);
}
catch (Exception e) {
}
}
Run Code Online (Sandbox Code Playgroud)
但我觉得这个说法并不正确。try catch 是因为它对我大喊有关 get() 上未处理的异常,所以我不知道如何解决这个问题。
我现在也尝试过这个:
@Mock
final CompletableFuture<List<String>> mockedFuture = Mockito.mock(CompletableFuture.class);
Run Code Online (Sandbox Code Playgroud)
设置中包含以下内容:
Mockito.doReturn(new ArrayList<Row>()).when(mockedFuture).get();
Run Code Online (Sandbox Code Playgroud)
但它仍然无休止地挂着。
我看过这些:
如何在 Mockito 中模拟完成 CompletableFuture 这个我不明白它到底想让我做什么,并且感觉不太适用,因为它不是一种 get 方法。我在这里看到了一些例子,其中有 .get() ...但不幸的是,没有一个是模拟方法,它们是在测试本身中获取的: https: //www.javatips.net/api/java.util.concurrent.completablefuture
编辑:代码运行。它返回结果。所以并不是说实际的方法没有返回值——我知道它是这样做的,它现在正在 QA 中这样做。
您的当前CompletableFuture尚未完成,因此该.get()方法挂起等待永远不会发生的异步完成。您可以使用CompletableFuture.completedFuture(value)创建一个实例,该实例在调用CompletableFuture时将返回传递的值。.get()
| 归档时间: |
|
| 查看次数: |
18960 次 |
| 最近记录: |