1

In Mockito if you want a void method to do nothing you can do this:

doNothing().when(_mockedClass).voidMethod();

Is there a way to do this with JMockit?

I can't seem to find anything about it. I've been trying to switch to JMockit but am having trouble finding documentation for some of the things we do with Mockito. I suspect they are all there in one form or another, just having trouble finding them, so figured I'd start asking one question at a time here and hope there are easy answers! Thanks!

Mimerr
  • 390
  • 1
  • 5
  • 14
  • 1
    Void methods do nothing by default with a mockito mock. – bric3 Nov 18 '14 at 07:56
  • Do they? I have a logger, where you will call void methods to log things, so like: logger.error("logthis"); While these don't actually cause problems in the test it does still log these things even though the logger is mocked. I got around this with the above 'do nothing' approach to stop it from logging these things. So are you saying it should not have been logging at all if I had it mocked correctly? – Mimerr Nov 19 '14 at 13:46
  • yes it does nothing otherwise it's not a mock (it's a spy or a partial mock (partial mock are not good thing)). Maybe the method is `final` which is why it cannot yet be mocked with mockito. – bric3 Nov 19 '14 at 15:53

1 Answers1

1

Just like with Mockito, actually, if you want a method to do nothing, then simply don't record an expectation for it. Alternatively, you can record an expectation with no result, but there is really no need for that (or a point to it).

(This would only not be the case when using strict expectations (with new StrictExpectations() {{ ... }}), in which case all method invocations need to be accounted for.)

Rogério
  • 16,171
  • 2
  • 50
  • 63