PersistenceUnitDecorator implements TestMethodDecorator { @Override public void beforeTest(final TestInvocation invocation) throws Exception { final EntityManagerFactory emf = (EntityManagerFactory) invocation.getContext().getData(Constants.KEY_ENTITY_MANAGER_FACTORY); final Class<?> fieldType = invocation.getContext().getPersistenceField().getType(); if (fieldType.equals(EntityManagerFactory.class)) { injectValue(invocation.getTestInstance().get(), invocation.getContext().getPersistenceField(), emf); } }  @Override int getPriority(); @Override void beforeTest(final TestInvocation invocation); @Override void afterTest(final TestInvocation invocation); @Override boolean isConfigurationSupported(final ExecutionContext ctx);  }
@Test public void testApplyEntityManagerFactoryInjection() throws Throwable { final Field field = getClass().getDeclaredField("emf"); when(ctx.getPersistenceField()).thenReturn(field); final PersistenceUnitDecorator fixture = new PersistenceUnitDecorator(); fixture.beforeTest(invocation); assertThat(emf, equalTo(entityManagerFactory)); verify(entityManagerFactory, times(0)).createEntityManager(); verify(entityManagerFactory, times(0)).close(); }
@Test public void testApplyForAnUnsupportedField() throws Throwable { final Field field = getClass().getDeclaredField("someField"); when(ctx.getPersistenceField()).thenReturn(field); final PersistenceUnitDecorator fixture = new PersistenceUnitDecorator(); fixture.beforeTest(invocation); assertThat(emf, nullValue()); verify(entityManagerFactory, times(0)).createEntityManager(); verify(entityManagerFactory, times(0)).close(); }