MongoDbDecorator implements TestMethodDecorator { @Override public void afterTest(final TestInvocation invocation) throws Exception { final ExecutionContext context = invocation.getContext(); final MongoDatabase mongoDb = (MongoDatabase) context.getData(Constants.KEY_MONGO_DB); final MongoDbFeatureExecutor dbFeatureExecutor = (MongoDbFeatureExecutor) context.getData(Constants.KEY_FEATURE_EXECUTOR); context.storeData(Constants.KEY_MONGO_DB, null); context.storeData(Constants.KEY_FEATURE_EXECUTOR, null); dbFeatureExecutor.executeAfterTest(mongoDb, invocation.getException().isPresent()); }  @Override int getPriority(); @Override void beforeTest(final TestInvocation invocation); @Override void afterTest(final TestInvocation invocation); @Override boolean isConfigurationSupported(final ExecutionContext ctx);  }
@Test public void testAfterTest() throws Throwable { when(invocation.getException()).thenReturn(Optional.empty()); decorator.afterTest(invocation); verify(executor).executeAfterTest(eq(mongoDataBase), eq(Boolean.FALSE)); verify(ctx).storeData(eq(Constants.KEY_MONGO_DB), isNull()); verify(ctx).storeData(eq(Constants.KEY_FEATURE_EXECUTOR), isNull()); }
@Test public void testMongoClientIsClosedEvenIfExecuteAfterTestFails() throws Throwable { when(invocation.getException()).thenReturn(Optional.of(new Exception())); doThrow(RuntimeException.class).when(executor).executeAfterTest(any(MongoDatabase.class), anyBoolean()); try { decorator.afterTest(invocation); fail("Exception expected"); } catch (final Exception e) { } verifyZeroInteractions(mongoClient); }