java NoAMockException 而 Mockito.verify(Object,VerificationMode.atleast(2))

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16818772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:04:54  来源:igfitidea点击:

NoAMockException while Mockito.verify(Object,VerificationMode.atleast(2))

javatestngmockitojunit-ee

提问by Ramesh

I am using mockito for mock the unit test cases and am getting the following exception

我正在使用 mockito 来模拟单元测试用例并收到以下异常

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to verify() is of type ConsumerImpl and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
    verify(mock).someMethod();
    verify(mock, times(10)).someMethod();
    verify(mock, atLeastOnce()).someMetenter code herehod();

and my code is

我的代码是

 MessageConsumer mConsumer = Mockito.mock(MessageConsumer.class);
            String data = "new Message for Testing";
            Message message = new Message(data.getBytes());
            Mockito.when(mConsumer.next(10, TimeUnit.SECONDS)).thenReturn(message);
            StringParserTest parserTest = new StringParserTest();
            ConsumerImpl<String> consumer = new ConsumerImpl<String>(mConsumer, parserTest);
            String mes=Mockito.verify(consumer,VerificationModeFactory.times(3)).consumeMessage(10,TimeUnit.SECONDS);

Please some one help me to solve this problem

请有人帮我解决这个问题

Thanks in Advance

提前致谢

SRN

SRN

回答by Brice

Well, that's exactly what mockito says, you are not passing a mock to verify !

好吧,这正是 mockito 所说的,您没有通过模拟来验证!

ConsumerImpl<String> consumer = new ConsumerImpl<String>(mConsumer, parserTest);
String mes=Mockito.verify(consumer,VerificationModeFactory.times(3)).consumeMessage(10,TimeUnit.SECONDS);

Plus if you verified a mock why would you want to store the result of the invocation you verify, it wouldn't make sense since the consumer is mocked. Verify is to verify calls on mocked objects that are the collaborators of your unit tested object. Which in your case is not really clear.

另外,如果您验证了一个模拟,为什么要存储您验证的调用的结果,因为消费者被模拟了,所以没有意义。验证是验证对作为单元测试对象协作者的模拟对象的调用。在你的情况下,这不是很清楚。

Also you never use your mock mConsumerinstance.

你也从不使用你的模拟mConsumer实例。

You should definitely separate your test in 3 phase, one for the fixture, one for the action, and one for the verifications. Use the BDD terminology to achieve that, it augments understanding and readability for the tester and future reader of this code (And Mockito offers them in the API through BDDMockito).

你绝对应该把你的测试分成 3 个阶段,一个用于夹具,一个用于动作,一个用于验证。使用 BDD 术语来实现这一点,它增强了测试人员和此代码的未来读者的理解和可读性(并且 Mockito 通过 BDDMockito 在 API 中提供它们)。

As I don't really get what the code is trying to test from the code you gave, I'll be imagining things. So for example you'll write this kind of code (using import static) :

由于我并没有真正从您提供的代码中获得代码试图测试的内容,因此我将进行想象。例如,您将编写这种代码(使用import static):

// given a consumer
MessageConsumer message_consumer = mock(MessageConsumer.class);
String the_message_data = "new Message for Testing";
given(message_consumer.next(10, SECONDS)).willReturn(new Message(the_message_data.getBytes()));

// when calling the client of the customer (which is the unit that is tested)
new MessageProcessor(message_consumer).processAll();

// then verify that consumeMessage is called 3 times
verify(message_consumer, times(3)).consumeMessage(10, SECONDS);

Remember Mockito helps you focus on interactions between objects — as it's the most important notion of object oriented programming — and especially between the tested one and his collaborators that will certainly be mocked.

记住 Mockito 可以帮助您专注于对象之间的交互——因为它是面向对象编程中最重要的概念——尤其是在被测试者和他的合作者之间,他们肯定会被嘲笑。

回答by Sumit Sundriyal

Usually we mock using @InjectMock and we try to verify a method called from inside the test case method. Here is one scenario which generally give issue.

通常我们使用@InjectMock 进行模拟,并尝试验证从测试用例方法内部调用的方法。这是一种通常会出现问题的场景。

public class A{
  @Autowired
  Service s

  public void method1(){
    method2();
  }
  public void method2(){
    s.someMethod();
  }
}

public class ATest{

  @InjectMocks
  A a;

  public void testM1(){
    a.method1();
    Mockito.verify(a, Mockito.times(1)).method2();

  }
}

This will always give "NoAMockException while Mockito.verify" instead of that we should use following verification.

这将始终给出“NoAMockException while Mockito.verify”,而不是我们应该使用以下验证。

public class ATest{
  @InjectMocks
  A a;
  @Mock
  Service s

  public void testM1(){
    a.method1();
    Mockito.verify(s, Mockito.times(1)).someMethod();
  }
}

Or if we want to verify() method2() then we have to @Mock class A instead of @InjectMock

或者如果我们想要 verify() method2() 那么我们必须@Mock class A 而不是@InjectMock