### Question:
ElasticsearchItemWriter implements ItemWriter<IndexQuery>, InitializingBean { @Override public void write(List<? extends IndexQuery> items) throws Exception { if(isActualTransactionActive()) { addToBuffer(items); }else { writeItems(items); } } ElasticsearchItemWriter(ElasticsearchOperations elasticsearchOperations); void setDelete(boolean delete); @Override void afterPropertiesSet(); @Override void write(List<? extends IndexQuery> items);  }### Answer:
@Test public void shouldNotWriteWhenNoTransactionIsActiveAndNoItem() throws Exception { writer.write(null); verifyZeroInteractions(elasticsearchOperations); writer.write(new ArrayList<IndexQuery>(0)); verifyZeroInteractions(elasticsearchOperations); }
@Test public void shouldWriteItemWhenNoTransactionIsActive() throws Exception { IndexQueryBuilder builder = new IndexQueryBuilder(); builder.withObject(dummyDocument); List<IndexQuery> items = asList(builder.build()); writer.write(items); verify(elasticsearchOperations).index(items.iterator().next()); }
@Test public void shouldWriteItemWhenInTransaction() throws Exception { IndexQueryBuilder builder = new IndexQueryBuilder(); builder.withObject(dummyDocument); final List<IndexQuery> items = asList(builder.build()); transactionTemplate.execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { try { writer.write(items); } catch (Exception e) { fail("An error occurred while writing: " + e.getMessage()); } return null; } }); verify(elasticsearchOperations).index(items.iterator().next()); }
@Test public void shouldNotWriteItemWhenTransactionFails() throws Exception { IndexQueryBuilder builder = new IndexQueryBuilder(); builder.withObject(dummyDocument); final List<IndexQuery> items = asList(builder.build()); try { transactionTemplate.execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { try { writer.write(items); } catch (Exception ignore) { fail("unexpected error occurred"); } throw new RuntimeException("rollback"); } }); } catch (RuntimeException re) { } catch (Throwable t) { fail("Unexpected error occurred"); } verifyZeroInteractions(elasticsearchOperations); }
@Test public void shouldNotWriteItemWhenTransactionIsReadOnly() throws Exception { IndexQueryBuilder builder = new IndexQueryBuilder(); builder.withObject(dummyDocument); final List<IndexQuery> items = asList(builder.build()); try { transactionTemplate.setReadOnly(true); transactionTemplate.execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { try { writer.write(items); } catch (Exception ignore) { fail("unexpected error occurred"); } return null; } }); } catch (Throwable t) { fail("unexpected error occurred"); } verifyZeroInteractions(elasticsearchOperations); }