模拟对象下的模拟对象

时间:2015-05-13 13:23:22

标签: java junit mockito

我需要模拟一个模拟对象的实例。我试过了,但是这个对象是空的。

基本上我需要尝试尽可能多地调用实际实现,第二级模拟不会发生。

任何想法的人,怎么做。

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 个答案:

答案 0 :(得分:0)

模拟不是真实对象,因此它没有needtomock字段。您可能希望在此处使用已编程的行为(Mockito.when()等)或使用带有模拟MyUtil的真实mockundertest实例

一些阅读:https://github.com/mockito/mockito/wiki/FAQ

相关问题