简体   繁体   English

Mockito NotaMockException

[英]Mockito NotaMockException

I am facing an issue with Mockito junit testing.我正面临 Mockito junit 测试的问题。 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;
   }
}

Getting exception :获取异常:

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

in the below code在下面的代码中

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

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.我知道活动不是模拟,但我不确定有没有办法解决这个问题,因为secondMethod()是同一个类中的方法。 I need to write rule for secondMethod() as I have already done its Unit Testing.我需要为secondMethod()编写规则,因为我已经完成了它的单元测试。 The definition of secondMethod() consists has external dependencies. secondMethod()的定义包含外部依赖。 Should I be mocking the external dependencies present in secondMethod() and writing rules for them rather than rule for secondMethod() ?我应该嘲笑secondMethod()存在的外部依赖项并为它们编写规则而不是为secondMethod()编写规则吗?

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.我找到了这篇文章: Mockito Spy'ing on the object being unit testing 然而,将 secondMethod() 分成不同的类没有意义。 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.即使使用 spy() 模拟实际类也不是最正确的方法,正如帖子中已经解释的那样。

I don't think I should be creating a mock of the Activity class as that is the class I am testing.我认为我不应该创建 Activity 类的模拟,因为这是我正在测试的类。 I would really appreciate help and insights into this.我真的很感激对此的帮助和见解。

As you noted, act is not a mock, and therefore you cannot record behavior on it.正如您所指出的, act不是模拟,因此您无法在其上记录行为。 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 .你可以使用Mockito.spy ,好了,间谍(或部分模拟)的act对象,以便只录制的行为secondMethod和执行实际代码firstMethod

Note, however, that matchers can't be used in doReturn calls regardles of how you're mock ing or spy ing your object.但是请注意,无论您如何mockspy对象,都不能在doReturn调用中使用匹配器。 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();
  }
} 

A slightly more elegant syntax allows you to use annotations instead of explicitly calling Mockito.spy , but it's a matter of taste really:稍微优雅的语法允许您使用注释而不是显式调用Mockito.spy ,但这确实是一个品味问题:

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

Here are some hints:这里有一些提示:

  1. Mock the Activity.模拟活动。
  2. Tweak the behavior of secondMethod with when / then / doReturn使用 when / then / doReturn 调整 secondMethod 的行为
  3. Use doCallRealMethod when firstMethod is invoked.调用 firstMethod 时使用 doCallRealMethod。

Hope it helps.希望能帮助到你。

There is no reason to mock anything in this example.在这个例子中没有理由嘲笑任何东西。 Since there are no dependencies and both methods are public, you can test them directly.由于没有依赖项并且两个方法都是公共的,因此您可以直接测试它们。

public class ActivityTest() {

    private Activity act = new Activity();

    @Test
    public void testSecondMethod(){
        assertEquals("expected-value", act.secondMethod());
    }

    @Test
    public void testFirstMethod() {
        act.firstMethod();
        // success if no exception occurs
    }
} 

Since firstMethod does not have any detectable effect on the Act instance, nor on any dependency (since there are none) you can simply call the method and be satisfied if no exception is thrown.由于 firstMethod 对 Act 实例和任何依赖项(因为没有)都没有任何可检测的影响,因此您可以简单地调用该方法,如果没有抛出异常就可以满足。 One could also reason that such a method should not be tested at all.人们也可以推断根本不应该测试这种方法。

I assume the example given is a simplification of a class where calling firstMethod actually does have side effects, who knows...我假设给出的例子是一个类的简化,其中调用 firstMethod 实际上确实有副作用,谁知道...

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

相关问题 使用Mockito NotaMockException进行Junit测试 - Junit test with Mockito NotaMockException Mockito验证Class对象NotAMockException - Mockito verify on Class object NotAMockException Mockito使用spring mvc注入抛出NotAMockException - Mockito throws NotAMockException using spring mvc injection org.mockito.exceptions.misusing.NotAMockException - org.mockito.exceptions.misusing.NotAMockException NotAMockException 而 Mockito.verify(Object,VerificationMode.atleast(2)) - NotAMockException while Mockito.verify(Object,VerificationMode.atleast(2)) InjectMocks 对象上的 org.mockito.exceptions.misusing.NotAMockException - org.mockito.exceptions.misusing.NotAMockException on InjectMocks object mock-maker-inline 使测试失败,并在非最终非静态 class 上“传递给 Mockito.mockingDetails() 的参数应该是模拟的” - mock-maker-inline makes test fail with “NotAMockException Argument passed to Mockito.mockingDetails() should be a mock” on non-final non-static class 在下面的测试用例上获取 NotAMockException - Getting NotAMockException on the below Test Case 在 Calendar.getInstance() 上获取 NotAMockException mocking - Getting NotAMockException on Calendar.getInstance() mocking 尝试验证 static 方法时出现 NotAMockException - NotAMockException when trying to verify a static method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM