2017-08-14 67 views
0

因此,它是我第一次使用EasyMock,我正在尝试为一些遗留代码添加一些单元测试。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>&bull;", 
       "<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); 
结果

为空!

是的,这将是无效的,如果它实际上将被执行,因为在数据库中不存在与clientNumber没有记录= 1和commentCategory = 1

这就是为什么,我想嘲讽说,DAO,并迫使它的返回一个CommentBO对象。

正如我上面所说,它是我第一次使用EasyMock,所以我在这里错过了一些明显的东西?我是否需要模拟dao方法中的调用(即AssessmentDao的getComment)?但是如果我这样做,我会强制嘲笑标准对象等,我认为这是不好的做法?

回答

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>&bull;", "<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)); 
    } 

} 
+0

好吧我编辑了代码。 AssessmentService.getComment不是一个静态调用。它应该是assessmentService.getComment。我尝试着放入EasyMock.verify(assessmentDao)并评论断言部分。测试通过成功,所以这意味着评估Dao正在被调用。然而,我仍然想测试这个断言部分--- assertThat(bo,instanceOf(CommentBO.class)) –

+0

我想补充一下,当我创建一个有效的CommentBO时,意思是当我创建一个具有clientNumber的CommentBO和评论分类存在于数据库,断言工程(assertThat(bo,instanceOf(CommentBO.class))),但这是什么让我更混乱,因为我认为我嘲笑(或我是否存根?)assessmentDao.getComment返回CommentBO(无论传递给assessmentDao.getComment方法的clientnumber和commentcategory的值如何) –

+0

您的属性名为'assessmentDao',您正在从'creditAssessmentDao'获得评论。我认为他们是一样的,但也许他们不是。另外,请使用'createMock',而不是'createNiceMock'来查看验证失败。 – Henri