9

I am attempting to convert a test suite which uses JMockit to use Mockito & powermock.

In the test setup there is the following snippet of code:

 new MockUp<Controller>() {
     @Mock
     public boolean sendMessage(final String string1, final String string2) {
        queue.add(string1);
        return true;
     }
  };

I am guessing that this means whenever that method is called during testing, then use this mocked implementation. Is this correct?

Also is there an equivalent to MockUp in Mockito or Powermock?

Thank you!

Mat
  • 515
  • 3
  • 8
  • 13
  • Correct, it means exactly that. And as far as I know, there is no `MockUp` equivalent in any other mocking API. – Rogério Jul 24 '14 at 15:39

1 Answers1

-1

Rather than try to translate JMockit code directly, its better to rewrite the tests using Mockito idioms.

Unit tests which use mockito usually follow this form:

  1. Create mock objects (often done in setUp, or with annotations)
  2. stub any necessary methods using Mockito.when
  3. Invoke the code that is being tested.
  4. Make any assertions about the state and/or return values of the code being tested.
  5. Verify expected interactions using Mockito.verify

I don't know what MockUp actually does, but this looks like it is part of a stub. If you still need to call queue.add, then you can do that in an Answer. If that queue.add was simply used to validate the string1 for each call, then you can do that without a queue.

Daniel
  • 4,481
  • 14
  • 34
  • Okay, so I just noticed this question is 6 years old. My bad :-). Anyway, I stand by my answer anyway. – Daniel Apr 24 '20 at 15:14