private FieldInitializationReport initializeInjectMocksField(Field field, Object fieldOwner) { FieldInitializationReport report = null; try { report = new FieldInitializer(fieldOwner, field).initialize(); } catch (MockitoException e) { if (e.getCause() instanceof InvocationTargetException) { Throwable realCause = e.getCause().getCause(); new Reporter().fieldInitialisationThrewException(field, realCause); } new Reporter().cannotInitializeForInjectMocksAnnotation(field.getName(), e); } return report; // never null }
@Test public void should_throw_ClassCastException_on_second_call() throws Exception { doInNewChildRealm( testMethodClassLoaderRealm, "org.mockitousage.configuration.ClassCacheVersusClassReloadingTest$DoTheMocking"); try { doInNewChildRealm( testMethodClassLoaderRealm, "org.mockitousage.configuration.ClassCacheVersusClassReloadingTest$DoTheMocking"); fail("should have raised a ClassCastException when Objenesis Cache is enabled"); } catch (MockitoException e) { assertThat(e.getMessage()) .containsIgnoringCase("classloading") .containsIgnoringCase("objenesis") .containsIgnoringCase("MockitoConfiguration"); assertThat(e.getCause()) .satisfies(thatCceIsThrownFrom("java.lang.Class.cast")) .satisfies( thatCceIsThrownFrom( "org.mockito.internal.creation.jmock.ClassImposterizer.imposterise")); } }