### Question:
HostComponentDesiredStateDAO { @Transactional public void remove(HostComponentDesiredStateEntity hostComponentDesiredStateEntity) { HostEntity hostEntity = hostComponentDesiredStateEntity.getHostEntity(); if (hostEntity == null) { throw new IllegalStateException(String.format("Missing hostEntity for host id %1d", hostComponentDesiredStateEntity.getHostId())); } hostEntity.removeHostComponentDesiredStateEntity(hostComponentDesiredStateEntity); entityManagerProvider.get().remove(hostComponentDesiredStateEntity); hostDAO.merge(hostEntity); }  @RequiresSession HostComponentDesiredStateEntity findById(long id); @RequiresSession List<HostComponentDesiredStateEntity> findAll(); @RequiresSession HostComponentDesiredStateEntity findByServiceComponentAndHost(String serviceName, String componentName, String hostName); @RequiresSession HostComponentDesiredStateEntity findByIndex(Long clusterId, String serviceName,
                                                     String componentName, Long hostId); @RequiresSession List<HostComponentDesiredStateEntity> findByIndex(Long clusterId, String serviceName,
                                                     String componentName); @RequiresSession List<HostComponentDesiredStateEntity> findByHostsAndCluster(Collection<Long> hostIds, Long clusterId); @Transactional void refresh(HostComponentDesiredStateEntity hostComponentDesiredStateEntity); @Transactional void create(HostComponentDesiredStateEntity hostComponentDesiredStateEntity); @Transactional HostComponentDesiredStateEntity merge(HostComponentDesiredStateEntity hostComponentDesiredStateEntity); @Transactional void remove(HostComponentDesiredStateEntity hostComponentDesiredStateEntity); @Transactional void removeId(long id);  }### Answer:
@Test public void testRemove() throws Exception { Provider<EntityManager> entityManagerProvider = createNiceMock(Provider.class); EntityManager entityManager = createNiceMock(EntityManager.class); HostDAO hostDAO = createNiceMock(HostDAO.class); HostEntity hostEntity = createNiceMock(HostEntity.class); HostComponentDesiredStateEntity hostComponentDesiredStateEntity = createNiceMock(HostComponentDesiredStateEntity.class); expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes(); entityManager.remove(hostComponentDesiredStateEntity); hostEntity.removeHostComponentDesiredStateEntity(hostComponentDesiredStateEntity); expect(hostDAO.merge(hostEntity)).andReturn(hostEntity).atLeastOnce(); expect(hostComponentDesiredStateEntity.getHostEntity()).andReturn(hostEntity).atLeastOnce(); replay(entityManagerProvider, entityManager, hostDAO, hostEntity, hostComponentDesiredStateEntity); HostComponentDesiredStateDAO dao = new HostComponentDesiredStateDAO(); dao.entityManagerProvider = entityManagerProvider; dao.hostDAO = hostDAO; dao.remove(hostComponentDesiredStateEntity); verify(entityManagerProvider, entityManager, hostDAO, hostEntity, hostComponentDesiredStateEntity); }