### Question:
AbstractTeardownService implements TeardownService { Exception addTestRef(@NonNull Exception e, @NonNull AbstractTestDataEntity testDataRef) { if (!(e instanceof SakuliException)) { e = new SakuliCheckedException(e); } ((SakuliException) e).setAsyncTestDataRef(testDataRef); return e; }  @Override void handleTeardownException(@NonNull Exception e, boolean async, @NonNull AbstractTestDataEntity testDataRef); @Override void teardownTestCase(@NonNull TestCase testCase); @Override void teardownTestCaseStep(@NonNull TestCaseStep testCaseStep);  }### Answer:
@Test public void testHandleTeardownExceptionSequential() { testling.tearDown(Optional.of(testsuite)); verify(exceptionHandler).handleException(any(SakuliRuntimeException.class)); verify(testling, never()).addTestRef(any(), any()); List<Exception> resumeExceptions = (List<Exception>) ReflectionTestUtils.getField(exceptionHandler, "resumeExceptions"); assertTrue(resumeExceptions.stream().noneMatch(e -> e.getClass().isAssignableFrom(SakuliRuntimeException.class))); assertEquals(testsuite.getException().getClass(), SakuliRuntimeException.class); assertRegExMatch(testsuite.getException().toString(), "Method 'teardownTestSuite' is not implemented for forwarder class.*"); }
@Test public void testHandleTeardownExceptionAsync() { final TestCase testCase = testsuite.getTestCases().values().stream().findFirst().get(); testling.tearDown(Optional.of(testCase), true); verify(exceptionHandler).handleException(any(SakuliRuntimeException.class)); verify(testling).addTestRef(any(SakuliRuntimeException.class), eq(testCase)); List<Exception> resumeExceptions = (List<Exception>) ReflectionTestUtils.getField(exceptionHandler, "resumeExceptions"); assertTrue(resumeExceptions.stream().allMatch(e -> e.getClass().isAssignableFrom(SakuliRuntimeException.class))); assertNull(testsuite.getException()); assertEquals(testCase.getException().getClass(), SakuliRuntimeException.class); assertEquals(testCase.getException().toString(), "teardown test case"); }
@Test public void testAddTestRefSakuliException() { @NonNull Exception e = new SakuliForwarderRuntimeException(""); final Exception result = testling.addTestRef(e, testsuite); assertEquals(result.getClass(), SakuliForwarderRuntimeException.class); assertEquals(((SakuliException) result).getAsyncTestDataRef().get(), testsuite); }
@Test public void testAddTestRefNoSakuliException() { @NonNull Exception e = new RuntimeException(""); final Exception result = testling.addTestRef(e, testsuite); assertEquals(result.getClass(), SakuliCheckedException.class); assertEquals(result.getCause(), e); assertEquals(((SakuliException) result).getAsyncTestDataRef().get(), testsuite); }