### Question:
DataStoreImpl implements DataStore { @Override public <T> Collection<T> findAll(Class<T> clazz, String whereClause) throws PersistenceException { checkInitialize(); EntityManager em = getEntityManager(); try { Collection<T> resources = new HashSet<>(); DynamicType type = getDynamicEntityType(clazz); if (type != null) { try { Query query = em.createQuery(getSelectStatement(clazz, whereClause)); List dynamicEntities = query.getResultList(); for (Object dynamicEntity : dynamicEntities) { resources.add(toEntity(clazz, type, (DynamicEntity) dynamicEntity)); } } catch (Exception e) { throwPersistenceException("Caught exception trying to find " + clazz.getName() + " where " + whereClause, e); } } return resources; } finally { em.close(); } }  @Override void store(Object entity); @Override void remove(Object entity); @Override T find(Class<T> clazz, Object primaryKey); @Override Collection<T> findAll(Class<T> clazz, String whereClause);  }### Answer:
@Test public void testFindAll() throws Exception { DynamicClassLoader classLoader = new DynamicClassLoader(DataStoreImplTest.class.getClassLoader()); JpaEntityManager jpaEntityManager = createMock(JpaEntityManager.class); ServerSession session = createMock(ServerSession.class); DatabaseLogin databaseLogin = createMock(DatabaseLogin.class); EntityManagerFactory entityManagerFactory = createMock(EntityManagerFactory.class); EntityManager entityManager = createMock(EntityManager.class); JPADynamicHelper jpaDynamicHelper = createNiceMock(JPADynamicHelper.class); SchemaManager schemaManager = createNiceMock(SchemaManager.class); DynamicEntity dynamicEntity = createMock(DynamicEntity.class); Query query = createMock(Query.class); PowerMock.mockStatic(JpaHelper.class); expect(JpaHelper.getEntityManager(entityManager)).andReturn(jpaEntityManager).anyTimes(); PowerMock.replay(JpaHelper.class); expect(jpaEntityManager.getServerSession()).andReturn(session).anyTimes(); expect(session.getLogin()).andReturn(databaseLogin).anyTimes(); Capture<Sequence> sequenceCapture = EasyMock.newCapture(); databaseLogin.addSequence(capture(sequenceCapture)); EasyMock.expectLastCall().anyTimes(); Capture<DynamicType> typeCapture = EasyMock.newCapture(); Capture<DynamicType> typeCapture2 = EasyMock.newCapture(); jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2)); expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes(); expect(entityManager.createQuery( "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_id=99")).andReturn(query); entityManager.close(); expect(query.getResultList()).andReturn(Collections.singletonList(dynamicEntity)); expect(dynamicEntity.get("DS_id")).andReturn(99); expect(dynamicEntity.get("DS_name")).andReturn("foo"); TestSubEntity subEntity = new TestSubEntity("bar"); expect(dynamicEntity.get("DS_subEntity")).andReturn(subEntity); replay(entityManagerFactory, entityManager, jpaDynamicHelper, dynamicEntity, query, schemaManager, jpaEntityManager, session, databaseLogin); DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager); Collection<TestEntity> entities = dataStore.findAll(TestEntity.class, "id=99"); Assert.assertEquals(1, entities.size()); TestEntity entity = entities.iterator().next(); Assert.assertEquals(99, (int) entity.getId()); Assert.assertEquals("foo", entity.getName()); verify(entityManagerFactory, entityManager, jpaDynamicHelper, dynamicEntity, query, schemaManager, jpaEntityManager, session, databaseLogin); }