freepascal / mockito

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

verify(mock, lastParametersUsed()).someMethod(p1, p2, p3); #86

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have written some code that is very inconvenient to test with Mockito but
still relatively simple to understand: http://pastebin.com/f65dc2735

What makes this difficult to test is the fact that Button.setFocused gets
called so many times in so many ways for every method that exists. For
example, calling ButtonGroup.init(); ButtonGroup.focusNext(); calls
button.setFocused() many times.  

At the end of the test, the only thing I really care about is the last
parameters passed into button.setFocused().  For example, if I have 3
buttons and I call ButtonGroup.init(); ButtonGroup.focusNext();, All I
really care about is that the first and third had setVisible(false); called
last and the second had setVisible(true); called last.  So, I'd like to
request a verification type for this.  It would look like this, for this test:

verify(button1, lastParametersUsed()).setVisible(false);
verify(button2, lastParametersUsed()).setVisible(true);
verify(button3, lastParametersUsed()).setVisible(false);

Original issue reported on code.google.com by tie...@gmail.com on 24 May 2009 at 2:41

Attachments:

GoogleCodeExporter commented 9 years ago
I wanted to make this an enhancement, but I couldn't figure out how to do that, 
sorry.

Original comment by tie...@gmail.com on 24 May 2009 at 2:41

GoogleCodeExporter commented 9 years ago
What if you considered not using mocks for this case? Are you interested on if 
or how
many times setVisible() method is called? Or are you interested in the state of 
the
buttons - whether they are visible or not?

assertEquals(false, button1.isVisible());
assertEquals(true, button2.isVisible());
assertEquals(false, button3.isVisible());

Original comment by szcze...@gmail.com on 24 May 2009 at 10:03

GoogleCodeExporter commented 9 years ago
Yes, using Fakes instead of Mocks would make this test as simple as your 
suggestion:

assertEquals(false, button1.isVisible());
assertEquals(true, button2.isVisible());
assertEquals(false, button3.isVisible());

In the end, I resorted to this. But, should Mocks settle at being good for some 
tests
and Fakes being good at other tests? Wouldn't it be better if Mocks were good 
at all
tests?  I was suggesting a way that a Mock could be used just as easily as a 
Fake for
the type of test it's normally weak at.

Original comment by tie...@gmail.com on 25 May 2009 at 12:45

GoogleCodeExporter commented 9 years ago
>Yes, using Fakes instead of Mocks would make this test as simple as your 
suggestion

So what about real objects? Cannot you use real buttons by any chance?

>But, should Mocks settle at being good for some tests
>and Fakes being good at other tests?

I guess my division is: sometimes all the family of mocks is better (spy, mock,
dummy, etc.) and many times real object is better.

Original comment by szcze...@gmail.com on 25 May 2009 at 7:59

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 9 Jul 2009 at 12:48

GoogleCodeExporter commented 9 years ago
Starting with Mockito 1.8.0 you can use ArgumentCaptor for that:

ArgumentCaptor<Boolean> argument = ArgumentCaptor.forClass(Boolean.class);
verify(button1, atLeast(1)).setVisible(argument.capture());
assertFalse(argument.getValue());

Original comment by bbankow...@gmail.com on 25 Aug 2009 at 2:02

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 11 Nov 2009 at 1:33

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 11 Nov 2009 at 2:35