我有这段代码:
CompletableFuture<SomeClass> future = someInstance.getSomething(-902);
try {
future.get(15, TimeUnit.SECONDS);
fail("Print some error");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
// Here I want to check if e.getCause() matches some exception
} catch (TimeoutException e) {
e.printStackTrace();
}
因此,当抛出ExecutionException时,它会被另一个类中的另一个异常抛出。我想检查导致ExecutionException的原始异常是否匹配我创建的一些自定义异常。如何使用JUnit实现这一目标?
答案 0 :(得分:2)
像这样使用import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
FPSCLOCK = pygame.time.Clock()
RED = pygame.Color("red")
startpoint = pygame.math.Vector2(320, 240)
endpoint = pygame.math.Vector2(170, 0)
angle = 0
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# % 360 to keep the angle between 0 and 360.
angle = (angle+5) % 360
# The current endpoint is the startpoint vector + the
# rotated original endpoint vector.
current_endpoint = startpoint + endpoint.rotate(angle)
screen.fill((0, 0, 0))
pygame.draw.line(screen, RED, startpoint, current_endpoint, 2)
pygame.display.flip()
FPSCLOCK.tick(30)
pygame.quit()
sys.exit()
:
ExpectedException
答案 1 :(得分:1)
很简单,您可以使用&#34;内置&#34;解决这个问题。事情(规则很好,但这里不需要):
catch (ExecutionException e) {
assertThat(e.getCause(), is(SomeException.class));
换句话说:只是取得那个原因;然后断言需要断言的东西。 (我使用的是assertThat和is()匹配器;请参阅here进一步阅读)