所以我第一次使用EasyMock,我正在尝试为一些遗留代码添加一些单元测试。
遗留代码在Spring 3.1中,我正在使用EasyMock 3.4。
我在这里要完成的是测试一个调用dao的服务(一种用Spring编写的方法)。
以下是代码:
@Entity
@Table(name="comment")
public class CommentBO{
public static CommentBO createNewComment(Integer clientNumber, Integer commentCategory){
CommentBO bo = new CommentBO();
bo.setClientNumber(clientNumber);
bo.setCommentCategory(commentCategory);
return bo;
}
}
public interface AssessmentService {
public CommentBO getComment(Integer clientNumber, Integer
commentCategory);
}
public class AssessmentServiceImpl implements
AssessmentService {
@Resource(name = "com.client.assessment.bl.dao.AssessmentDao")
private AssessmentDao assessmentDao;
@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public CommentBO getComment(Integer clientNumber,Integer commentCategory) {
CommentBO comment = this.assessmentDao.getComment(
clientNumber, commentCategory);
if (comment != null && comment.getComments() != null) {
comment.setComments(comment.getComments().replaceAll("<li>•",
"<li>"));
}
return comment;
}
public interface AssessmentDao {
public CommentBO getComment(Integer clientNumber, Integer commentCategory);
}
@Repository(value = "com.client.assessment.bl.dao.AssessmentDao")
public class AssessmentDaoImpl implements AssessmentDao {
@Override
public CommentBO getComment(Integer clientNumber, Integer
commentCategory) {
Criteria criteria =
this.getSession(false).createCriteria(CommentBO.class);
criteria.add(Restrictions.eq("clientNumber", clientNumber))
.add(Restrictions.eq("commentCategory", commentCategory));
if (criteria.list() != null && criteria.list().size() > 0) {
return (CommentBO) criteria.list().get(0);
}
return null;
}
}
这是我用EasyMock编写的单元测试
@SpringApplicationContext("classpath*:/com/client/assessment/**/*-context.xml")
public class AssessmentServiceTest extends UnitilsJUnit4 {
@SpringBean("com.client.assessment.remote.AssessmentService")
public AssessmentService assessmentService = null;
@Test
public void testGetComment(){
Integer clientNumber = 1;
Integer commentCategory = 1;
CommentBO commentBO = CommentBO.createNewComment(clientNumber, commentCategory);
AssessmentDao assessmentDao = EasyMock.createMock(AssessmentDao.class);
EasyMock.expect(assessmentDao.getComment((Integer) anyObject(), (Integer) anyObject())).andReturn(commentBO).anyTimes();
ReflectionTestUtils.setField(assessmentService, "assessmentDao", assessmentDao);
EasyMock.replay(assessmentDao);
CommentBO bo = assessmentService.getComment(clientNumber, commentCategory);
assertThat( bo , instanceOf(CommentBO.class));
}
}
所以基本上发生了什么,我的单元测试失败了,因为
的结果assessmentService.getComment(clientNumber, commentCategory);
是空的!
是的,如果实际执行它将为null,因为在数据库中没有clientNumber = 1和commentCategory = 1的记录。
这就是为什么,我想到嘲笑所说的dao并强迫它返回一个CommentBO对象。
正如我上面所说,我第一次使用EasyMock,所以我错过了一些明显的东西吗?我是否需要在dao方法中模拟调用(即evaluateDao的getComment)?但是,如果我这样做,我将强制嘲笑Criteria对象等,我认为这是不好的做法?
答案 0 :(得分:1)
当前代码应该有效。所以我看到的唯一可能性是getComment
实际上是最终的。如果删除anyTimes
并在断言之前添加verify
,则应该会看到缺少呼叫。
唯一的另一种可能性是ReflectionTestUtils
无声地失败。所以你仍然注入了原始的春豆。如果你调试,你会很容易看到服务中注入的DAO不是模拟。
我在下面重写了你的代码。它完美无缺。
public interface AssessmentDao {
CommentBO getComment(Integer clientNumber, Integer commentCategory);
}
public class AssessmentDaoImpl implements AssessmentDao {
@Override
public CommentBO getComment(Integer clientNumber, Integer commentCategory) {
throw new AssertionError("Should not be called");
}
}
public interface AssessmentService {
CommentBO getComment(Integer clientNumber, Integer commentCategory);
}
public class AssessmentServiceImpl implements AssessmentService {
private AssessmentDao assessmentDao;
@Override
public CommentBO getComment(Integer clientNumber, Integer commentCategory) {
CommentBO comment = this.assessmentDao.getComment(clientNumber, commentCategory);
if (comment != null && comment.getComments() != null) {
comment.setComments(comment.getComments().replaceAll("<li>•", "<li>"));
}
return comment;
}
}
public class CommentBO {
public static CommentBO createNewComment(Integer clientNumber, Integer commentCategory) {
CommentBO bo = new CommentBO();
bo.setClientNumber(clientNumber);
bo.setCommentCategory(commentCategory);
return bo;
}
private Integer clientNumber;
private Integer commentCategory;
private String comments;
public Integer getClientNumber() {
return clientNumber;
}
public void setClientNumber(Integer clientNumber) {
this.clientNumber = clientNumber;
}
public Integer getCommentCategory() {
return commentCategory;
}
public void setCommentCategory(Integer commentCategory) {
this.commentCategory = commentCategory;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
}
public class ReflectionTestUtils {
public static void setField(Object object, String fieldName, Object value) {
try {
Field field = object.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(object, value);
}
catch(Exception e) {
throw new RuntimeException(e);
}
}
}
public class AssessmentServiceTest {
public AssessmentService assessmentService = new AssessmentServiceImpl();
@Test
public void testGetComment(){
Integer clientNumber = 1;
Integer commentCategory = 1;
CommentBO commentBO = CommentBO.createNewComment(clientNumber, commentCategory);
AssessmentDao assessmentDao = EasyMock.createMock(AssessmentDao.class);
expect(assessmentDao.getComment(anyObject(), anyObject())).andReturn(commentBO);
ReflectionTestUtils.setField(assessmentService, "assessmentDao", assessmentDao);
replay(assessmentDao);
CommentBO bo = assessmentService.getComment(clientNumber, commentCategory);
verify(assessmentDao);
assertThat(bo , instanceOf(CommentBO.class));
}
}