模拟对象下的java模拟对象
我需要模拟一个模拟对象的实例。我试过了,但是这个对象是空的
基本上,我需要尽可能多地调用真正的实现,第二级模拟没有发生
伙计们,有什么好主意吗
public class mytest{
@Mock
Myutil myutil;
@InjectMock
ClassunderTest ct
@Before
setup()
{
MockitoAnotation.initMocks(ct);
}
@Test
test1(){
when(myutil.dotask()).thenReturn("");
ct.m1();
}
}
public class ClassunderTest{
@inject
MyUtil u1;
public string m1(){
u1.dotask();
}
}
public class MyUtil(){
@inject
mockunderMock needtomock; //this instance needs to be mocked
public dotask(){
needtomock.play()
}
}
# 1 楼答案
mock不是真正的对象,因此它没有
needtomock
字段。您可能希望在这里使用编程行为(Mockito.when()
等),或者使用带有模拟mockundertest
的真实MyUtil
实例一些阅读:https://github.com/mockito/mockito/wiki/FAQ