Using "reset" in tests

791 views
Skip to first unread message

Frédéric Delanoy

unread,
Dec 3, 2008, 12:28:19 PM12/3/08
to PowerMock
Hi,

I'd like, in a single test, to vary input arguments ("sub-tests"), and
verify behaviour accordingly

I try to use the EasyMock.reset(<mocks>) to be able to re-use them,
but it doesn't work.

More precisely, I've sthg like

@Test
public void test_method() throws Exception {
// A
MockedClass mock = createPartialMock(MockedClass.class,
"jni_method_name");
// expectations
replayAll();
// test code
verifyAll();
// checks results
// B

}

This works and runs OK.

However, when I add at the end
org.easymock.EasyMock.reset(mock)
and recopy the [A-B] code afterwards, it fails
with a java.lang.UnsupportedOperationException:
java.lang.IllegalArgumentException: not a proxy instance
at java.lang.reflect.Proxy.getInvocationHandler(Unknown Source)
at org.easymock.EasyMock.getControl(EasyMock.java:1600)
at org.easymock.EasyMock.reset(EasyMock.java:1515)
at MockedClassTest.test_method(MockedClassTestjava:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
Using org.easymock.classextension.EasyMock.reset(mock) instead yields:
java.lang.UnsupportedOperationException: jni_method_name is native
at MockedClass.jni_method_name(MockedClass.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...

By checking Powermock API, I've found no trace of any reset() method.
Is that currently impossible to do, or is there another procedure to
follow ?

Rgrds,

Frédéric

Johan Haleby

unread,
Dec 4, 2008, 1:15:30 AM12/4/08
to powe...@googlegroups.com
That's true we haven't implemented the reset method. The reason is simply that we're not using it ourselves. I think it's better if you split your test into two different tests with good naming instead of using reset. But if you like a reset functionality I'll implement it in PowerMock as well.

/Johan

Frédéric Delanoy

unread,
Dec 4, 2008, 2:52:11 AM12/4/08
to PowerMock
Well, there's probably a balance to make between test number in a test
class (hence
easier detection of failed tests) and the level of granularity that
shouldn't be overly
fine-grained IMHO.

For instance, I generally like, when test feature A, to do sthg like
for (valid_input: valid_inputs_classes)
testInputOK
for (invalid_input: invalid_inputs_classes)
testInputKO

So yes, it would be nice to have "reset" implemented.

Thanks anyway for this great soft!

Frédéric

On 4 déc, 07:15, "Johan Haleby" <johan.hal...@gmail.com> wrote:
> That's true we haven't implemented the reset method. The reason is simply
> that we're not using it ourselves. I think it's better if you split your
> test into two different tests with good naming instead of using reset. But
> if you like a reset functionality I'll implement it in PowerMock as well.
>
> /Johan
>
> On Wed, Dec 3, 2008 at 6:28 PM, Frédéric Delanoy <frederic.dela...@gmail.com

Johan Haleby

unread,
Dec 4, 2008, 4:02:17 AM12/4/08
to powe...@googlegroups.com
Ok I've implemented this functionality now and committed it to trunk. I've added three new methods to the PowerMock API, reset(Object...mocks), reset(Class<?>...classMocks) and resetAll(). The methods can be used to reset instance mocks, class mocks or let PowerMock reset all mocks automatically. If you don't wish to check out and build from the source you could use the reset method from the EasyMock class extensions API for instance mocks (even if they're created by PowerMock). So if this is enough for you then I suggest that you use this until we've released the next version of PowerMock.

Hope this helps.
/Johan

Frédéric Delanoy

unread,
Dec 4, 2008, 10:13:05 AM12/4/08
to PowerMock
Thanks!

On Dec 4, 10:02 am, "Johan Haleby" <johan.hal...@gmail.com> wrote:
> Ok I've implemented this functionality now and committed it to trunk. I've
> added three new methods to the PowerMock API, reset(Object...mocks),
> reset(Class<?>...classMocks) and resetAll(). The methods can be used to
> reset instance mocks, class mocks or let PowerMock reset all mocks
> automatically. If you don't wish to check out and build from the source you
> could use the reset method from the EasyMock class extensions API for
> instance mocks (even if they're created by PowerMock). So if this is enough
> for you then I suggest that you use this until we've released the next
> version of PowerMock.
>
> Hope this helps.
> /Johan
>
> On Thu, Dec 4, 2008 at 8:52 AM, Frédéric Delanoy <frederic.dela...@gmail.com

Jan Kronquist

unread,
Dec 6, 2008, 6:55:03 AM12/6/08
to powe...@googlegroups.com
Great work Johan! I have a hard time just keeping up with all the nice features you are adding! :-)

/Jan
Reply all
Reply to author
Forward
0 new messages