TypeCachingJaxb implements Jaxb { public <T> T unmarshal(Class<T> type, Type genericType, Reader reader) throws Exception { Set<Class<?>> classes = Types.getActors(genericType); classes.add(type); Jaxb jaxb = get(classes); return jaxb.<T>unmarshal(type, genericType, reader); } TypeCachingJaxb(CRestConfig crestConfig, Class<?> source); void marshal(T object, OutputStream out, Charset charset); T unmarshal(Class<T> type, Type genericType, Reader reader);  }
@Test public void shouldCreateJaxbInstanceForFirstCallFromClassAndTypeAndReuseItForUnmarshallCallWithSameObjectType() throws Exception { Type type1 = Types.newType(List.class, Types.newType(Set.class, int[].class)); Class clazz1 = List.class; Type type2 = String.class; Class clazz2 = String.class; Reader reader = mock(Reader.class); Jaxb mockJaxb1 = mock(Jaxb.class); Jaxb mockJaxb2 = mock(Jaxb.class); Class[] expected = asSet(List.class, Set.class, int[].class).toArray(new Class[3]); mockStatic(JaxbFactory.class); when(JaxbFactory.create(mockCRestConfig, getClass(), expected)).thenReturn(mockJaxb1); when(JaxbFactory.create(mockCRestConfig, getClass(), new Class<?>[]{String.class})).thenReturn(mockJaxb2); toTest.unmarshal(clazz1, type1, reader); toTest.unmarshal(clazz1, type1, reader); toTest.unmarshal(clazz2, type2, reader); toTest.unmarshal(clazz2, type2, reader); verify(mockJaxb1, times(2)).unmarshal(clazz1, type1, reader); verify(mockJaxb2, times(2)).unmarshal(clazz2, type2, reader); }