ssacher-tgm / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Mockito.when() on a method should override default mock behaviour to its class #206

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

1. create a class with a method that throws an Exception
2. mock that class with default behavior to call real methods
3. in a test, use Mockito.when() on the method that throws exception (to 
override its behavior)
4. run the test that applies the Mockito.when() on the mocked class

What is the expected output? What do you see instead?

Using when() on a method that throws an exception should override the default 
mocking behavior, even if this is call real method. Trying to setup your mock 
should not call the real method.

What version of the product are you using? On what operating system?

Mockito 1.8.4, WindowsXP

Please provide any additional information below.

Original issue reported on code.google.com by Alexandr...@gmail.com on 27 Jul 2010 at 10:22

Attachments:

GoogleCodeExporter commented 8 years ago
Hey,

Unfortunately it cannot be fixed due to how java works. It is related to the 
order of evaluation of statements in java: arguments are evaluated first.

In this case please consider using doReturn() syntax - it does not have this 
issue.

Original comment by szcze...@gmail.com on 27 Jul 2010 at 10:31

GoogleCodeExporter commented 8 years ago
but, inside the when() method, the mock is called, not the real method. 

can't the mock behavior (in this case, "call the real deal") be triggered 
lazily. can't the when() method tell the mock "ok you are good to go" after it 
registered everything ? what about putting a proxy around the mock ?

Original comment by Alexandr...@gmail.com on 27 Jul 2010 at 3:05

GoogleCodeExporter commented 8 years ago
As Szczepan mentions, it's impossible. Imagine you want to call your real 
method. How would that differ from using it in when()? There is no way to 
distinguish it.

Original comment by bbankow...@gmail.com on 27 Jul 2010 at 3:17

GoogleCodeExporter commented 8 years ago
Although we believe it is impossible, @Alexandru, you're welcome to try fixing 
it :)

Original comment by szcze...@gmail.com on 27 Jul 2010 at 3:41