简体   繁体   English

模拟对象在模拟对象下

[英]Mock object under mocked object

I need to mock the instance of a mocked object. 我需要模拟一个模拟对象的实例。 I tried, but the object in coming as null. 我试过了,但是对象为null。

Basically I need to try to call the real implementation as much as possible, the second level of mocking is not happening. 基本上,我需要尝试尽可能多地调用真实的实现,第二级模拟并没有发生。

Any idea guys, how to do it. 任何有想法的人,怎么做。

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()
   }
}

A mock is not a real object so it doesn't have a needtomock field. 模拟不是真正的对象,因此它没有needtomock字段。 You probably want to use a programmed behavior here ( Mockito.when() etc) or use a real MyUtil instance with a mocked mockundertest 您可能想要在此处使用编程的行为( Mockito.when()等),或将真实的MyUtil实例与mockundertest

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM