@Test
public void createExceptionReleasesPooledElement() throws IOException, PoolResourceException {

    // given
    ItemSourcePool mockedPool = mock(ItemSourcePool.class);
    ByteBuf byteBuf = createDefaultTestByteBuf();
    ItemSource<ByteBuf> bufferedItemSource = spy(new ByteBufItemSource(byteBuf, source -> {}));
    when(mockedPool.getPooled()).thenReturn(bufferedItemSource);
    PooledItemSourceFactory pooledItemSourceFactory = new PooledItemSourceFactory(mockedPool);
    LogEvent logEvent = mock(LogEvent.class);
    ObjectWriter objectWriter = spy(new ObjectMapper().writerFor(LogEvent.class));
    doThrow(new IOException("test exception")).when(objectWriter).writeValue(any(OutputStream.class), eq(logEvent));
    ItemSource<ByteBuf> result = null;
    Exception caught = null;
    // when
    try {
        result = pooledItemSourceFactory.create(logEvent, objectWriter);
    } catch (Exception e) {
        caught = e;
    }
    // then
    assertNull(result);
    verify(bufferedItemSource).release();
    assertEquals(IllegalArgumentException.class, caught.getClass());
}