### Question:
ExtendedObjectWriter extends ObjectWriter { @Override protected DefaultSerializerProvider _serializerProvider() { if (this.serializerProvider == null) { serializerProvider = super._serializerProvider(); } if (_serializerProvider.cachedSerializersCount() > lastCacheSize) { lastCacheSize = _serializerProvider.cachedSerializersCount(); serializerProvider = super._serializerProvider(); } return serializerProvider; } ExtendedObjectWriter(ObjectMapper objectMapper, SerializationConfig serializationConfig);  ExtendedObjectWriter(ObjectMapper objectMapper, SerializationConfig serializationConfig, FormatSchema formatSchema);  ExtendedObjectWriter(ObjectMapper objectMapper, SerializationConfig config, JavaType rootType, PrettyPrinter pp);   }### Answer:
@Test public void returnsSameSerProvWhenNoNewSerializersCached() throws IOException { int expectedCalls = new Random().nextInt(100) + 2; List<DefaultSerializerProvider> caught = new ArrayList<>(); ExtendedObjectMapper mapper = new ExtendedObjectMapper(new JsonFactory()); ExtendedObjectWriter writer = spy((ExtendedObjectWriter)mapper._newWriter(mapper.getSerializationConfig(), mapper.getTypeFactory().constructArrayType(Object.class), null)); when(writer._serializerProvider()).thenAnswer((Answer<DefaultSerializerProvider>) invocationOnMock -> { DefaultSerializerProvider result = (DefaultSerializerProvider) invocationOnMock.callRealMethod(); caught.add(result); return result; }); for (int i = 0; i < expectedCalls; i++) { writer.writeValue((OutputStream)new ReusableByteBufOutputStream(Mockito.mock(ByteBuf.class)), null); } Assert.assertThat(caught, CoreMatchers.everyItem(CoreMatchers.is(caught.get(0)))); verify(writer, Mockito.times(expectedCalls))._serializerProvider(); }
@Test public void returnsNewSerProvNewWhenSerializersCached() throws IOException { int expectedCalls = new Random().nextInt(100) + 2; List<DefaultSerializerProvider> caught = new ArrayList<>(); ExtendedObjectMapper mapper = new ExtendedObjectMapper(new JsonFactory()); ExtendedObjectWriter writer = spy((ExtendedObjectWriter)mapper._newWriter(mapper.getSerializationConfig(), mapper.getTypeFactory().constructType(Log4jLogEvent.class), null)); when(writer._serializerProvider()).thenAnswer((Answer<DefaultSerializerProvider>) invocationOnMock -> { DefaultSerializerProvider result = (DefaultSerializerProvider) invocationOnMock.callRealMethod(); caught.add(result); return result; }); for (int i = 0; i < expectedCalls; i++) { writer.writeValue((OutputStream)new ReusableByteBufOutputStream(Mockito.mock(ByteBuf.class)), Log4jLogEvent.newBuilder().build()); } Assert.assertEquals(2, new HashSet<>(caught).size()); verify(writer, Mockito.times(expectedCalls))._serializerProvider(); }