GroupDAO { @RequiresSession public GroupEntity findGroupByName(String groupName) { final TypedQuery<GroupEntity> query = entityManagerProvider.get().createNamedQuery("groupByName", GroupEntity.class); query.setParameter("groupname", groupName.toLowerCase()); try { return query.getSingleResult(); } catch (NoResultException e) { return null; } }  @RequiresSession GroupEntity findByPK(Integer groupPK); @RequiresSession List<GroupEntity> findAll(); @RequiresSession GroupEntity findGroupByName(String groupName); @RequiresSession GroupEntity findGroupByNameAndType(String groupName, GroupType groupType); @RequiresSession List<GroupEntity> findGroupsByPrincipal(List<PrincipalEntity> principalList); @RequiresSession GroupEntity findGroupByPrincipal(PrincipalEntity principal); @Transactional void create(GroupEntity group); @Transactional void create(Set<GroupEntity> groups); @Transactional GroupEntity merge(GroupEntity group); @Transactional void merge(Set<GroupEntity> groups); @Transactional void remove(GroupEntity group); @Transactional void remove(Set<GroupEntity> groups); @Transactional void removeByPK(Integer groupPK);  }
@Test public void testfindGroupByName() { final String groupName = "engineering"; final GroupEntity entity = new GroupEntity(); entity.setGroupName(groupName); TypedQuery<GroupEntity> query = createStrictMock(TypedQuery.class); expect(entityManager.createNamedQuery(eq("groupByName"), eq(GroupEntity.class))).andReturn(query); expect(query.setParameter("groupname", groupName)).andReturn(query); expect(query.getSingleResult()).andReturn(entity); replay(entityManager, query); final GroupDAO dao = new GroupDAO(); dao.entityManagerProvider = entityManagerProvider; final GroupEntity result = dao.findGroupByName(groupName); assertSame(entity, result); verify(entityManagerProvider, entityManager, query); }