2017-08-14 70 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