### Question:
HibernateUtils { public static SessionFactory getSessionFactory(@NonNull MetadataSources sources) { try { Metadata metadata = sources.getMetadataBuilder().build(); return metadata.getSessionFactoryBuilder().build(); } catch (Exception e) { if (sources.getServiceRegistry() != null) { StandardServiceRegistryBuilder.destroy(sources.getServiceRegistry()); } throw e; } } private  HibernateUtils(); static StandardServiceRegistry createServiceRegistry(@NonNull Map<String, String> settings,
      @NonNull PoolConfiguration configuration); static SessionFactory getSessionFactory(@NonNull MetadataSources sources); static FlushMode setFlushMode(@NonNull Session session,
      @NonNull TransactionDefinition transactionDefinition); static void resetFlushMode(@NonNull Session session, FlushMode flushMode);  }### Answer:
@Test public void testSessionFactory() { assertNotNull(getSessionFactory()); }
@Test public void testSessionFactoryExceptionally() { StandardServiceRegistry serviceRegistry = getServiceRegistry(); when(sources.getMetadataBuilder()).thenThrow(new RuntimeException()); assertThrows(RuntimeException.class, () -> HibernateUtils.getSessionFactory(sources)); assertTrue(((StandardServiceRegistryImpl) serviceRegistry).isActive()); when(sources.getServiceRegistry()).thenReturn(serviceRegistry); assertThrows(RuntimeException.class, () -> HibernateUtils.getSessionFactory(sources)); assertFalse(((StandardServiceRegistryImpl) serviceRegistry).isActive()); }