### Question:
DatabaseResultServiceImpl extends AbstractTeardownService implements ResultService { @Override public void teardownTestSuite(@NonNull TestSuite testSuite) throws RuntimeException { LOGGER.info("======= SAVE RESULTS TO DATABASE ======"); try { daoTestSuite.saveTestSuiteResult(); daoTestSuite.saveTestSuiteToSahiJobs(); if (!CollectionUtils.isEmpty(testSuite.getTestCases())) { for (TestCase tc : testSuite.getTestCasesAsSortedSet()) { daoTestCase.saveTestCaseResult(tc); LOGGER.info("... try to save all STEPS for test case '" + tc.getId() + "'!"); SortedSet<TestCaseStep> steps = tc.getStepsAsSortedSet(); if (!steps.isEmpty()) { daoTestCaseStep.saveTestCaseSteps(steps, tc.getDbPrimaryKey()); LOGGER.info("all STEPS for '" + tc.getId() + "' saved!"); } else { LOGGER.info("no STEPS for '\" + tc.getId() +\"'found => no STEPS saved in DB!"); } } } LOGGER.info("======= FINISHED: SAVE RESULTS TO DATABASE ======"); } catch (Exception e) { throw new SakuliForwarderRuntimeException( "error by saving the results to the database for: " + testSuite.toString(), e); } }  @Override int getServicePriority(); @Override void teardownTestSuite(@NonNull TestSuite testSuite);  }### Answer:
@Test public void testSaveResultsInDatabase() { when(testSuite.getTestCases()).thenAnswer(a -> null); testling.teardownTestSuite(testSuite); verify(daoTestSuite).saveTestSuiteResult(); verify(daoTestSuite).saveTestSuiteToSahiJobs(); verify(daoTestCase, never()).saveTestCaseResult(any(TestCase.class)); verify(daoTestCaseStep, never()).saveTestCaseSteps(any(SortedSet.class), anyInt()); }
@Test(expectedExceptions = SakuliForwarderException.class, expectedExceptionsMessageRegExp = "error by saving the results to the database.*") public void testSaveResultsInDatabaseHandleException() { doThrow(DataAccessResourceFailureException.class).when(daoTestSuite).saveTestSuiteResult(); testling.teardownTestSuite(testSuite); }
@Test public void testSaveResultsInDatabaseWithTestcases() { Integer tcPrimaryKey = 22; TestCase tc1 = mock(TestCase.class); TestCaseStep tcs1 = mock(TestCaseStep.class); when(tc1.getDbPrimaryKey()).thenReturn(tcPrimaryKey); SortedSet<TestCaseStep> tcStepList = new TreeSet<>(Collections.singleton(tcs1)); when(tc1.getStepsAsSortedSet()).thenReturn(tcStepList); TestCase tc2 = mock(TestCase.class); when(tc2.getSteps()).thenReturn(new ArrayList<>()); Map<String, TestCase> testCaseMap = new HashMap<>(); testCaseMap.put("1", tc1); testCaseMap.put("2", tc2); testSuite = new TestSuite(); testSuite.setTestCases(testCaseMap); testling.teardownTestSuite(testSuite); verify(daoTestSuite).saveTestSuiteResult(); verify(daoTestSuite).saveTestSuiteToSahiJobs(); verify(daoTestCase).saveTestCaseResult(tc1); verify(daoTestCase).saveTestCaseResult(tc2); verify(daoTestCase, times(2)).saveTestCaseResult(any(TestCase.class)); verify(daoTestCaseStep).saveTestCaseSteps(tcStepList, tcPrimaryKey); verify(daoTestCaseStep).saveTestCaseSteps(any(SortedSet.class), anyInt()); }