package com.vst.service;
import java.util.List;
import java.util.ArrayList;
import com.vst.service.BaseManagerTestCase;
import com.vst.dao.ReasonDao;
import com.vst.model.Reason;
import com.vst.service.impl.ReasonManagerImpl;
import org.jmock.Mock;
import org.springframework.orm.ObjectRetrievalFailureException;
public class ReasonManagerTest extends BaseManagerTestCase {
private final String reasonId = "1";
private ReasonManagerImpl reasonManager = new ReasonManagerImpl();
private Mock reasonDao = null;
protected void setUp() throws Exception {
super.setUp();
reasonDao = new Mock(ReasonDao.class);
reasonManager.setReasonDao((ReasonDao) reasonDao.proxy());
}
protected void tearDown() throws Exception {
super.tearDown();
reasonManager = null;
}
public void testGetReasons() throws Exception {
List results = new ArrayList();
Reason reason = new Reason();
results.add(reason);
// set expected behavior on dao
reasonDao.expects(once()).method("getReasons")
.will(returnValue(results));
List reasons = reasonManager.getReasons(null);
assertTrue(reasons.size() == 1);
reasonDao.verify();
}
public void testGetReason() throws Exception {
// set expected behavior on dao
reasonDao.expects(once()).method("getReason")
.will(returnValue(new Reason()));
Reason reason = reasonManager.getReason(reasonId);
assertTrue(reason != null);
reasonDao.verify();
}
public void testSaveReason() throws Exception {
Reason reason = new Reason();
// set expected behavior on dao
reasonDao.expects(once()).method("saveReason")
.with(same(reason)).isVoid();
reasonManager.saveReason(reason);
reasonDao.verify();
}
public void testAddAndRemoveReason() throws Exception {
Reason reason = new Reason();
// set required fields
// set expected behavior on dao
reasonDao.expects(once()).method("saveReason")
.with(same(reason)).isVoid();
reasonManager.saveReason(reason);
reasonDao.verify();
// reset expectations
reasonDao.reset();
reasonDao.expects(once()).method("removeReason").with(eq(new Long(reasonId)));
reasonManager.removeReason(reasonId);
reasonDao.verify();
// reset expectations
reasonDao.reset();
// remove
Exception ex = new ObjectRetrievalFailureException(Reason.class, reason.getReasonId());
reasonDao.expects(once()).method("removeReason").isVoid();
reasonDao.expects(once()).method("getReason").will(throwException(ex));
reasonManager.removeReason(reasonId);
try {
reasonManager.getReason(reasonId);
fail("Reason with identifier '" + reasonId + "' found in database");
} catch (ObjectRetrievalFailureException e) {
assertNotNull(e.getMessage());
}
reasonDao.verify();
}
}