Mocking java.lang.reflect.Field

807 views
Skip to first unread message

Hannu Leinonen

unread,
Sep 18, 2009, 12:18:56 AM9/18/09
to powe...@googlegroups.com
I have a test which will always complain about
"java.lang.IllegalStateException: no last call on a mock available".

Here's my test:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Field.class)
public class FieldMockTest {
@Test
public void test() {
final Field fieldMock = createMock(Field.class);
expect(fieldMock.getName()).andReturn("foo");
replay(fieldMock);
assertEquals("foo", new ClassUnderTest().foo(fieldMock));
verify(fieldMock);
}

private static final class ClassUnderTest {
public String foo(Field field) {
return field.getName();
}
}
}

The line failing is:

expect(fieldMock.getName()).andReturn("foo");

I'm using PowerMock 1.2.5, EasyMock 2.5.2 and Junit 4.4. I really have
no clue what I might be doing wrong.


Best Regards,
Hannu

Jan Kronquist

unread,
Sep 21, 2009, 2:48:47 AM9/21/09
to powe...@googlegroups.com
You probably have to add
@PrepareForTest(FieldMockTest.class)

This is a bug that will be fixed in 1.3

/Jan

Johan Haleby

unread,
Sep 21, 2009, 3:01:59 AM9/21/09
to powe...@googlegroups.com
I wouldn't consider it a bug, I'd call it an enhancement :)

Hannu Leinonen

unread,
Sep 21, 2009, 3:28:47 AM9/21/09
to powe...@googlegroups.com
I tried it and it doesn't help. I've tried the following combinations:
@PrepareForTest({Field.class, FieldMockTest.class})
@PrepareForTest(Field.class)
@PrepareForTest(FieldMockTest.class)

And all of them produce the same result. The weird thing is that the
result is the same even completely without @PrepareForTest.


-Hannu

Johan Haleby

unread,
Sep 21, 2009, 4:29:45 AM9/21/09
to powe...@googlegroups.com
Could you post the code that you're trying to test?

/Johan

Hannu Leinonen

unread,
Sep 21, 2009, 4:33:28 AM9/21/09
to powe...@googlegroups.com
Already posted, it's the inner class ClassUnderTest. :)

-Hannu

Johan Haleby

unread,
Sep 21, 2009, 4:30:48 AM9/21/09
to powe...@googlegroups.com
Oh you already have, sorry. I'll take a look at it as soon as I can.

/Johan

Johan Haleby

unread,
Sep 21, 2009, 4:44:23 AM9/21/09
to powe...@googlegroups.com
Yeah I saw that, sorry. Unfortunately I'm filled up with work atm so you'll have to come back to you later.

/Johan

Johan Haleby

unread,
Sep 21, 2009, 4:49:15 AM9/21/09
to powe...@googlegroups.com
Just one more thing, why do you need to mock Field at all? (it should work of course but it seems to me that there may be a better way to solve your actual problem).

Hannu Leinonen

unread,
Sep 21, 2009, 5:35:16 AM9/21/09
to powe...@googlegroups.com
Well, the code uses some reflection (for validation of fields). A
work-around would be to create a test object, and in the test use
reflection retrieve it's Field so that it would use a real Field object
instead of a mocked one.


-Hannu

Johan Haleby wrote:
> Just one more thing, why do you need to mock Field at all? (it should
> work of course but it seems to me that there may be a better way to
> solve your actual problem).
>
> On Mon, Sep 21, 2009 at 10:44 AM, Johan Haleby <johan....@gmail.com
> <mailto:johan....@gmail.com>> wrote:
>
> Yeah I saw that, sorry. Unfortunately I'm filled up with work atm so
> you'll have to come back to you later.
>
> /Johan
>
>
> On Mon, Sep 21, 2009 at 10:33 AM, Hannu Leinonen <hlei...@gmail.com
> <mailto:hlei...@gmail.com>> wrote:
>
>
> Already posted, it's the inner class ClassUnderTest. :)
>
> -Hannu
>
> Johan Haleby wrote:
> > Could you post the code that you're trying to test?
> >
> > /Johan
> >
> > On Mon, Sep 21, 2009 at 9:28 AM, Hannu Leinonen
> <hlei...@gmail.com <mailto:hlei...@gmail.com>
> > <mailto:hlei...@gmail.com <mailto:hlei...@gmail.com>>> wrote:
> >
> >
> > I tried it and it doesn't help. I've tried the following
> combinations:
> > @PrepareForTest({Field.class, FieldMockTest.class})
> > @PrepareForTest(Field.class)
> > @PrepareForTest(FieldMockTest.class)
> >
> > And all of them produce the same result. The weird thing
> is that the
> > result is the same even completely without @PrepareForTest.
> >
> >
> > -Hannu
> >
> > Jan Kronquist wrote:
> > > You probably have to add
> > > @PrepareForTest(FieldMockTest.class)
> > >
> > > This is a bug that will be fixed in 1.3
> > >
> > > /Jan
> > >
> > > On Fri, Sep 18, 2009 at 6:18 AM, Hannu Leinonen
> > <hlei...@gmail.com <mailto:hlei...@gmail.com>

Johan Haleby

unread,
Sep 21, 2009, 7:46:14 AM9/21/09
to powe...@googlegroups.com
Actually PowerMock 1.2.5 doesn't have support for mocking instance methods in final system classes (getName() in the Field class is one example of that). This has been implemented in the trunk version though. However I tried to run your test against the trunk version and I got a NPE. I know the reason behind this and it's quite complex to fix it, but I'll try to do it before we release 1.3. Perhaps I have to postpone it to 1.4 though.

/Johan

Hannu Leinonen

unread,
Sep 21, 2009, 9:51:16 AM9/21/09
to powe...@googlegroups.com
Allright. So meanwhile I have to work it around somehow. Thank you for
your helpful responses!


-Hannu

Johan Haleby wrote:
> Actually PowerMock 1.2.5 doesn't have support for mocking instance
> methods in final system classes (getName() in the Field class is one
> example of that). This has been implemented in the trunk version though.
> However I tried to run your test against the trunk version and I got a
> NPE. I know the reason behind this and it's quite complex to fix it, but
> I'll try to do it before we release 1.3. Perhaps I have to postpone it
> to 1.4 though.
>
> /Johan
>
> On Mon, Sep 21, 2009 at 11:35 AM, Hannu Leinonen <hlei...@gmail.com
> <mailto:hlei...@gmail.com>> wrote:
>
>
> Well, the code uses some reflection (for validation of fields). A
> work-around would be to create a test object, and in the test use
> reflection retrieve it's Field so that it would use a real Field object
> instead of a mocked one.
>
>
> -Hannu
>
> Johan Haleby wrote:
> > Just one more thing, why do you need to mock Field at all? (it should
> > work of course but it seems to me that there may be a better way to
> > solve your actual problem).
> >
> > On Mon, Sep 21, 2009 at 10:44 AM, Johan Haleby
> <johan....@gmail.com <mailto:johan....@gmail.com>
> > <mailto:johan....@gmail.com <mailto:johan....@gmail.com>>>
> wrote:
> >
> > Yeah I saw that, sorry. Unfortunately I'm filled up with work
> atm so
> > you'll have to come back to you later.
> >
> > /Johan
> >
> >
> > On Mon, Sep 21, 2009 at 10:33 AM, Hannu Leinonen
> <hlei...@gmail.com <mailto:hlei...@gmail.com>
Reply all
Reply to author
Forward
0 new messages