Java - Mockito - CompletableFutures - 测试方法的意外结果
Java - Mockito - CompletableFutures - Unexpected Result of Tested Method
我有一个非常奇怪的极端情况,未来每隔几千 运行 就没有按预期完成一次。我能够始终如一地触发故障的唯一方法是重复测试 运行 直到失败。
表示测试:
public class TestFunctionality(){
@Mock private MockIntegrationA mockIntegrationA;
@InjectMocks private MyHandler unitUnderTest;
@BeforeEach
public void init(){
MockitoAnnotations.openMocks(this);
}
@Test
public void testManagerFailThenSucceed(){
when(mockIntegrationA.callRestMethod(any())
.thenThrow(new RuntimeException(""))
.thenReturn(CompletableFuture.completedFuture(null));
CompletableFuture<Void> result = unitUnderTest.invokeMethod();
assertTrue(result.isCompletedExceptionally());
result = unitUnderTest.callFunction();
assertFalse(result.isCompletedExceptionally());
}
}
上面测试中的第一个断言是当测试确实变坏时出现的问题。是否有未正确清理的内容或导致第一个结果 未 异常完成的边缘情况的原因?
我无法确认评论作为答案,但@tgdavies 关于 runAsync 的评论是正确的。我将其更改为 completedFuture,现在无法触发此行为。非常感谢您的帮助!花了几个小时试图解决它
我有一个非常奇怪的极端情况,未来每隔几千 运行 就没有按预期完成一次。我能够始终如一地触发故障的唯一方法是重复测试 运行 直到失败。
表示测试:
public class TestFunctionality(){
@Mock private MockIntegrationA mockIntegrationA;
@InjectMocks private MyHandler unitUnderTest;
@BeforeEach
public void init(){
MockitoAnnotations.openMocks(this);
}
@Test
public void testManagerFailThenSucceed(){
when(mockIntegrationA.callRestMethod(any())
.thenThrow(new RuntimeException(""))
.thenReturn(CompletableFuture.completedFuture(null));
CompletableFuture<Void> result = unitUnderTest.invokeMethod();
assertTrue(result.isCompletedExceptionally());
result = unitUnderTest.callFunction();
assertFalse(result.isCompletedExceptionally());
}
}
上面测试中的第一个断言是当测试确实变坏时出现的问题。是否有未正确清理的内容或导致第一个结果 未 异常完成的边缘情况的原因?
我无法确认评论作为答案,但@tgdavies 关于 runAsync 的评论是正确的。我将其更改为 completedFuture,现在无法触发此行为。非常感谢您的帮助!花了几个小时试图解决它