### Question:
CategoryDAO extends DefaultDAOBase<Integer, Category> { public Category findByNameAndEnterprise(final String categoryName, final Enterprise enterprise) { Query query; if (enterprise == null) { query = getSession().createQuery(BY_NAME_AND_NO_ENTERPRISE); } else { query = getSession().createQuery(BY_NAME_AND_ENTERPRISE); query.setParameter("idEnterprise", enterprise.getId()); } query.setParameter("name", categoryName); List<Category> results = query.list(); if (!results.isEmpty()) { return results.get(0); } return null; } CategoryDAO();  CategoryDAO(final EntityManager entityManager); Category findDefault(); ArrayList<Category> findGlobalCategories(); ArrayList<Category> findLocalCategories(final Integer idEnterprise); Category findByNameAndEnterprise(final String categoryName, final Enterprise enterprise); boolean existsAnyWithName(final String name); boolean existsAnyOtherWithName(final Category category, final String name); static final String BY_NAME_AND_ENTERPRISE; static final String BY_NAME_AND_NO_ENTERPRISE; static final String BY_GLOBAL; static final String BY_LOCAL; }### Answer:
@Test public void testFindByName() { Category category = eg().createUniqueInstance(); ds().persistAll(category); CategoryDAO dao = createDaoForRollbackTransaction(); Category result = dao.findByNameAndEnterprise(category.getName(), null); assertNotNull(result); assertEquals(result.getName(), category.getName()); result = dao.findByNameAndEnterprise(category.getName() + "UNEXISTING", null); assertNull(result); }
@Test public void testFindByNameAndEnterprise() { Enterprise enterprise = enterpriseGenerator.createUniqueInstance(); Category category = eg().createUniqueInstance(); category.setEnterprise(enterprise); List<Object> entitiesToPersist = new ArrayList<Object>(); eg().addAuxiliaryEntitiesToPersist(category, entitiesToPersist); persistAll(ds(), entitiesToPersist, category); CategoryDAO dao = createDaoForRollbackTransaction(); Category result = dao.findByNameAndEnterprise(category.getName(), enterprise); assertNotNull(result); assertEquals(result.getName(), category.getName()); }