Mockito间谍方法不起作用 [英] Mockito spy method not working

查看:389
本文介绍了Mockito间谍方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mockito.spy方法很麻烦.

I'm in trouble with mockito.spy method.

我最近来到一个旧"项目,我的第一个任务是在其中添加模拟,并进行真实的单元测试:)

I'm recently arrived on a "old" project and my first task is to add mockito in it, and to do real unit test :)

该项目有很多概念问题,但这里不是重点;)

the project has many conception problems but its not the point here ;)

我解释了我的问题:

我上课

public class Tutu{
  public Tutu(){
  }
}

public class Toto{
  public Toto(){
  }
  public int executeToto(Tutu tutu){
    //do some stuff
    return 5;
  }
}

public class Titi{
  private Toto toto;

  public Titi(){
     this.toto = new Toto();     
  }

  public void executeTiti(){
      //do some stuff
      Tutu tutu = new Tutu();
      int ret = this.toto.executeToto(tutu);
      //do some stuff
  }
}

在我的测试类TitiTest.java中,我只想测试executeTiti,我不想测试executeToto东西,因为该类具有自己的测试类TotoTest.java.

in my test class TitiTest.java I want to test only executeTiti, I don't want to test executeToto stuff because this class has is own test class TotoTest.java.

但是正如您所看到的,toto是在titi构造函数中实例化的,所以我尝试这样的操作: (我也在测试中使用了PowerMock,所以我使用的是PowerMockRunner,但这似乎不是问题)

but as you can see, toto is instantiate in titi constructor so I try something like this: (I'm using PowerMock in my test too, so I'm using PowerMockRunner but it doesn't seem to be the problem)

@RunWith(PowerMockRunner.class)
public class TitiTest {

 @Test
 public void testExecuteTiti(){
   Toto toto = Mockito.spy(new Toto());
   Mockito.doReturn(2).when(toto).executeToto(Mockito.any(Tutu.class));

   Titi testedObject = new Titi();
   testedObject.executeTiti();
 }
}

但是真正的方法总是在每次调用并且ret = 5:(

but the real method is always calling and ret = 5 everytime :(

我想念什么吗?我在stackoverflow上阅读了许多有关此问题的文章,并尝试了所有解决方案,但由于我认为我做对了,因此它永远无法正常工作.

Do I miss something? I read many post about this on stackoverflow and try all solution but it's never work because I think I'm doing right thing.

我使用junit4.11/powermock1.5.4/mockito1.9.5

I use junit4.11/powermock1.5.4/mockito1.9.5

推荐答案

Toto toto = Mockito.spy(new Toto());

请记住,这是您在此行中创建的Toto实例上的间谍/存根,而不是每个新创建的Toto上的间谍/存根.因此,当您致电:

Bear in mind that this spies/stubs on the Toto instance you create in this line, and not every newly-created Toto. So when you call:

Titi testedObject = new Titi();
testedObject.executeTiti();

构造函数new Titi()本身会创建一个新的Toto实例,不受Mockito的影响,因此对this.toto.executeAction()的调用将始终返回5.

The constructor new Titi() itself creates a new instance of Toto, unaffected by Mockito, so that call to this.toto.executeAction() will always return 5.

因为您使用的是PowerMockito,所以可以选择将Toto的构造函数存根 :

Because you're running with PowerMockito, you do have the option of stubbing Toto's constructor:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Titi.class) // stub the calling class Titi, not Toto!
public class TitiTest {
  @Test public void testExecuteTiti() {
    Toto toto = Mockito.spy(new Toto());
    Mockito.doReturn(2).when(toto).executeToto(Mockito.any(Tutu.class));

    PowerMockito.whenNew(Toto.class).withAnyArguments().thenReturn(toto);

    Titi testedObject = new Titi();
    testedObject.executeTiti();
  }
}

但是我最喜欢的选择是为Titi创建一个辅助构造函数,以进行测试:

But the option I like the best is to create a secondary constructor for Titi, for testing:

public Titi(){
  this.toto = new Toto();     
}

/** For testing only. Uses the passed Toto instance instead of a new one. */
Titi(Toto toto){
  this.toto = toto;
}

然后只需要您像这样调整您的测试:

Which then only requires you to adjust your test like this:

@Test public void testExecuteTiti(){
  Toto toto = Mockito.spy(new Toto());
  Mockito.doReturn(2).when(toto).executeToto(Mockito.any(Tutu.class));

  Titi testedObject = new Titi(toto);
  testedObject.executeTiti();
}

这篇关于Mockito间谍方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆