### Question:
PostHibernateDao extends GenericDao<Post> implements PostDao { @Override public Post getLastPostFor(Branch branch) { List<Post> posts = (List<Post>) session() .getNamedQuery("getLastPostForBranch") .setParameter("branchId", branch.getId()) .setMaxResults(1) .list(); if (!posts.isEmpty()) { return posts.get(0); } return null; } PostHibernateDao(SessionFactory sessionFactory); Page<Post> getUserPosts(JCUser author, PageRequest pageRequest, List<Long> allowedBranchesIds); @Override Page<Post> getPosts(Topic topic, PageRequest pageRequest); @Override Post getLastPostFor(Branch branch); @Override List<Post> getLastPostsFor(List<Long> branchIds, int postCount); @Override void changeRating(Long postId, int changes);  }### Answer:
@Test public void testGetLastPostForBranch() { int size = 2; List<Post> posts = PersistedObjectsFactory.createAndSavePostList(size); Topic postsTopic = posts.get(0).getTopic(); Branch postsBranch = postsTopic.getBranch(); Post expectedLastPost = posts.get(0); ReflectionTestUtils.setField( expectedLastPost, "creationDate", new DateTime(2100, 12, 25, 0, 0, 0, 0)); session.save(expectedLastPost); Post actualLastPost = dao.getLastPostFor(postsBranch); assertNotNull(actualLastPost, "Last post in the branch is not found."); assertEquals(actualLastPost.getId(), expectedLastPost.getId(), "The last post in the branch is the wrong."); }
@Test public void testGetLastPostInEmptyBranch() { Topic topic = PersistedObjectsFactory.getDefaultTopic(); Branch branch = topic.getBranch(); branch.deleteTopic(topic); session.save(branch); assertNull(dao.getLastPostFor(branch), "The branch is empty, so last post mustn't be found"); }