### Question:
UserManagerBean implements UserManager { public User findByUsername(String username) { Query query = em.createNamedQuery("findUserByUsername"); query.setParameter("username", username); return (User) query.getSingleResult(); }  User create(String username, String password); User findByUsername(String username); void remove(User user); void update(User user);  }### Answer:
@Test public void findingUserByUsername() throws Exception { EntityManager em = createMock(EntityManager.class); Query q = createMock(Query.class); User user = createDummyUser(username); expect(em.createNamedQuery("findUserByUsername")) .andReturn(q); expect(q.setParameter("username", username)).andReturn(q); expect(q.getSingleResult()).andReturn(user); replay(em, q); UserManagerBean bean = new UserManagerBean(); Inject.bean(bean).with(em); Assert.assertEquals(user, bean.findByUsername(username)); verify(em, q); }