Mockito NotaMockException [英] Mockito NotaMockException

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

问题描述

我遇到了Mockito junit测试的问题.我是新手,对我所面临的问题有些困惑.任何帮助,将不胜感激.

I am facing an issue with Mockito junit testing. I am new to it and am a bit confused with the problem I am facing. Any help on this would be appreciated.

class Activity{

    public void firstMethod(){

      String str = secondMethod();
   }

    public String secondMethod(){
      String str = null;

      /*  some Code */

      return str;
   }
}

获取异常:

*org.mockito.exceptions.misusing.NotAMockException: 
 Argument passed to when() is not a mock!*

在下面的代码中

class ActivityTest(){

  Activity act;

  @Before
  public void setup(){
     act = new Activity();
  }

  @Test
  public void testFirstMethod(){

      Mockito.doReturn(Mockito.anyString()).when(act).secondMethod();
      act.firstMethod();
      verify(act).secondMethod();
  }
} 

我知道活动不是模拟的,但是我不确定解决方法,因为secondMethod()是同一类中的方法.我已经为secondMethod()编写了规则,因为我已经完成了其单元测试. secondMethod()的定义包含外部依赖关系.我是否应该嘲笑secondMethod()中存在的外部依赖关系,并为它们编写规则,而不是为secondMethod()编写规则?

I am aware that activity is not a mock but I am not sure for a way around this as secondMethod() is a method in the same class. I need to write rule for secondMethod() as I have already done its Unit Testing. The definition of secondMethod() consists has external dependencies. Should I be mocking the external dependencies present in secondMethod() and writing rules for them rather than rule for secondMethod()?

我发现了这篇文章: 对正在进行单元测试的对象进行Mockito间谍 但是,将secondMethod()分成不同的类是没有意义的.我的方法与此类有关.在我看来,创建不同的测试类是不合适的.如后所述,即使使用spy()模拟实际的类也不是最正确的方法.

I found this post: Mockito Spy'ing on the object being unit tested However separating the secondMethod() into a different class does not make sense. My method is related to this class. Creating a different class for testing does not seem right to me. Even mocking the actual class using spy() is not the most correct way as already explained in the post.

我不认为我应该创建Activity类的模拟,因为这是我正在测试的类.我将非常感谢您对此提供的帮助和见解.

I don't think I should be creating a mock of the Activity class as that is the class I am testing. I would really appreciate help and insights into this.

推荐答案

如前所述,act不是模拟,因此您无法在其上记录行为.您可以使用 Mockito.spy 来,好吧,监视(或部分模拟)act对象,以便仅记录secondMethod的行为并执行firstMethod的实际代码.

As you noted, act is not a mock, and therefore you cannot record behavior on it. You could use Mockito.spy to, well, spy (or partially mock) the act object so that you only record the behavior of secondMethod and execute the actual code for firstMethod.

但是请注意,关于mockspy对象的方式,不能在doReturn调用中使用匹配器.返回值必须是一个具体对象.

Note, however, that matchers can't be used in doReturn calls regardles of how you're mocking or spying your object. A return value must be a concrete object.

class ActivityTest() {

  Activity act;

  @Before
  public void setup(){
     act = Mockito.spy(new Activity()); // Here!
  }

  @Test
  public void testFirstMethod(){

      Mockito.doReturn("someString").when(act).secondMethod();
      act.firstMethod();
      verify(act).secondMethod();
  }
} 

稍微优雅一点的语法允许您使用批注而不是显式调用Mockito.spy,但这确实是个问题:

A slightly more elegant syntax allows you to use annotations instead of explicitly calling Mockito.spy, but it's a matter of taste really:

@RunWith(MockitoJUnitRunner.class)
class ActivityTest() {

  @Spy
  Activity act = new Activity();

  @Test
  public void testFirstMethod(){

      Mockito.doReturn("someString").when(act).secondMethod();
      act.firstMethod();
      verify(act).secondMethod();
  }
} 

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

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