[mockito] Spying on real objects does not work?

1,190 views
Skip to first unread message

Jagger

unread,
Dec 27, 2008, 12:31:28 PM12/27/08
to mockito
Consider following code.

import org.mockito.Mockito;

class Meter {

public void writeSettingValue(double value) {
if (value > getMaximumSettingValue() || value <
getMinimumSettingValue()) {
throw new IllegalArgumentException();
}
}

public double getMinimumSettingValue() {
throw new UnsupportedOperationException("NYI");
}

public double getMaximumSettingValue() {
throw new UnsupportedOperationException("NYI");
}
}

public class SpyingOnRealObjectMociktoExploit {

public static void main(String[] args) {
Meter meter = new Meter();
Meter spyMeter = Mockito.spy(meter);
Mockito.doReturn(0.).when(spyMeter).getMinimumSettingValue();
Mockito.doReturn(999.9).when(spyMeter).getMaximumSettingValue
();
spyMeter.writeSettingValue(0.);
}

}

Although I mocked getMinimumSettingValue() method I get
UnsupportedOperationException
from the real implementation. Did I get something wrong about spying
on real objects?

--
Best regards
Jagger

szczepiq

unread,
Dec 27, 2008, 6:49:55 PM12/27/08
to moc...@googlegroups.com
Hi,

Spying on real objects is not partial mocking... Check out the
javadocs and have a look at some older threads on this list. I should
probably update the faq as well.

Cheers
Szczepan Faber

Jagger

unread,
Dec 28, 2008, 6:37:49 AM12/28/08
to mockito
Hi,

ok, thanks for the answer. I guess I will go on here with
EasyMock class extension partial mocking. :-)

--
Best regards
Jagger

On 28 Gru, 00:49, szczepiq <szcze...@gmail.com> wrote:
> Hi,
>
> Spying on real objects is not partial mocking... Check out the
> javadocs and have a look at some older threads on this list. I should
> probably update the faq as well.
>
> Cheers
> Szczepan Faber
>

szczepiq

unread,
Dec 28, 2008, 8:03:01 AM12/28/08
to moc...@googlegroups.com
Sure, it's up to you.

If you're a true adventurer you might experiment a bit. What if you
try to write a test case with partial mocking, then you might create a
test case without partial mocking (some adjustments to the design
would be necessary). Then you could compare the design and the
readability/quality of test cases and get back to me...

Cheers,
Szczepan Faber

Jagger

unread,
Jan 2, 2009, 1:02:20 PM1/2/09
to mockito
I came to a conclusion that using either Mockito or EasyMock
in this context is just beating around the bush. I just simply
extended
the class and overrode the methods I wanted to mock partially.

Simpler and tidier.

--
Best regards
Jagger.

On 28 Gru 2008, 14:03, szczepiq <szcze...@gmail.com> wrote:
> Sure, it's up to you.
>
> If you're a true adventurer you might experiment a bit. What if you
> try to write a test case with partial mocking, then you might create a
> test case without partial mocking (some adjustments to the design
> would be necessary). Then you could compare the design and the
> readability/quality of test cases and get back to me...
>
> Cheers,
> Szczepan Faber
>
Reply all
Reply to author
Forward
0 new messages