【发布时间】:2015-09-17 20:25:33
【问题描述】:
假设我在各自的源文件夹/包中有以下类...
[src/myApp]
|_Employee «concrete»
|_Manager «abstract»
|_ManagerImpl «concrete» (Class Under Test)
|_Recruiter «abstract»
|_RecruiterImpl «concrete» (Collaborator)
...
public class ManagerImpl implements Manager {
...
private Recruiter recR;
...
public void growTeam( Object criteria ){
//...check preconditions
Employee newB = recR.srcEmployee( criteria );
//...whatever else
}
...
}
...
[test/myApp]
|_RecruiterStandIn «concrete»
|_ManagerImplTest
...
public class RecruiterStandIn implements Recruiter {
Map<Object, Employee> reSrcPool = new HashMap<>();
public RecruiterStandIn( ){
// populate reSrcPool with dummy test data...
}
public Employee srcEmployee( Object criteria ){
return reSrcPool.get( criteria );
}
}
...
public class ManagerImplTest {
...
// Class Under Test
private ManagerImpl mgr;
// Collaborator
private Recruiter recR = new RecruiterStandIn( );
...
public void testGrowTeam( ) {
//...
mgr.setRecruiter( recR );
mgr.growTeam( criteria );
// assertions follow...
}
...
}
...
这是我的问题:鉴于我有一个 RecruiterStandIn 的具体实现,它已经存在于代码库中用于测试目的(在 test范围)...
在上面的单元测试中也使用模拟会是多余的吗?
另外在以上单元测试?
...
...
@Mock
private Recruiter recR;
...
...
public void testGrowTeam( ) {
...
expect( recR.srcEmployee( blah) ).andReturn( blah )...
// exercising/assertions/validations as usual...
}
...
您可以放心地假设RecruiterStandIn 为上述单元测试的目的完成了被测类所要求的一切。也就是说,为了简单的答案/解释,没有必要将上面的场景过度复杂化,围绕维护存根和诸如此类的人为的 what-ifs。
提前致谢。
【问题讨论】:
标签: java unit-testing mocking data-access-layer