这是我的代码:
`public class Service {
callSomeService(ContextTpye1 c1,Parameter p){
//do something here
ContextTpye2 c2 = constructContext(c1,p);
if(c2.timeout()){
c2.audit();
}
}
}`
在单元测试中,初始化Service,Parameter和ContextTpye1。是否可以只模拟c2.audit()?我想在这里做什么。感谢。
答案 0 :(得分:0)
因此,假设constructContext(c1,p)
为public
,我将攻击问题的方法是在测试Service
类时监视Service
允许您模拟constructContext(c1,p)
{1}}并返回ContextType2
的模拟。
public class ServiceTest
{
private Service service = Mockito.spy(new Service());
private ContextType2 c2 = Mockito.mock(ContextType2.class);
@Test
public void testCallSomeService()
{
Mockito.when(service.constructContext(Mockito.eq(ContextType1.class), Mockito.eq(Parameter.class))).thenReturn(c2);
Mockito.when(c2.timeout()).thenReturn(true);
Mockito.when(c2.audit()).doNothing();
service.callSomeService(new ContextType1(), new Parameter());
}
}