@Mock annotation instead of PowerMockito.mock(Mocked.class)?

5,686 views
Skip to first unread message

glenviewjeff

unread,
Jun 5, 2011, 10:55:40 PM6/5/11
to powe...@googlegroups.com
I much prefer the readability of the annotated style of Mockito declarations like: @Mock Editable editable;

However I have some static/final classes that seem to require the @PrepareForTest( { Editable.class}) and editable = PowerMockito.mock(Editable.class); style code.

Is there any support in PowerMock for using the @Mock or alternate annotations in place of the PowerMockito.mock() calls?


Johan Haleby

unread,
Jun 6, 2011, 3:03:17 AM6/6/11
to powe...@googlegroups.com
You need to use @PrepareForTest( { Editable.class})but then you can use @Mock Editable editable to create and inject a mock instance. If I remember things correctly It only works instance mocks and not "static mocks".

/Johan
--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To view this discussion on the web visit https://groups.google.com/d/msg/powermock/-/Y3c0Zm1RRUUydThK.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.

glenviewjeff

unread,
Jun 6, 2011, 10:28:24 AM6/6/11
to powe...@googlegroups.com
The @Mock annotation appears not to work (I assume as designed) for final classes.  It sure would be nice if there were a @PowerMock or @FinalMock annotation that would perform the functional equivalent to editText = PowerMockito.mock(EditText.class);

Johan Haleby

unread,
Jun 6, 2011, 2:30:13 PM6/6/11
to powe...@googlegroups.com
It should already work. Have a look at the examples here.

/Johan


On 06/06/2011 04:28 PM, glenviewjeff wrote:
The @Mock annotation appears not to work (I assume as designed) for final classes.  It sure would be nice if there were a @PowerMock or @FinalMock annotation that would perform the functional equivalent to editText = PowerMockito.mock(EditText.class);
--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To view this discussion on the web visit https://groups.google.com/d/msg/powermock/-/T3VuMV9yc3BFQm9K.

glenviewjeff

unread,
Jun 6, 2011, 11:10:06 PM6/6/11
to powe...@googlegroups.com
Johan, thanks so much for getting back to me!  I'm not sure if this is a bug or a feature, but it ended up that the reason for the failures was that in my @Before SetUp() method I was calling

MockitoAnnotations.initMocks(this);

Which apparently causes the final mocking to fail.  Maybe this could be mentioned somewhere in the documentation if it can't somehow be deprecated to prevent developers from inadvertently calling it.

I had started this project with Mockito and didn't realize this call needs to be removed for PowerMock+Mockito.

Jeff


Johan Haleby

unread,
Jun 7, 2011, 1:36:15 AM6/7/11
to powe...@googlegroups.com
Ah I see, you're absolutely right that calling initMocks from Mockito will override the mocks created by PowerMock. Glad you sorted it out.

/Johan

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To view this discussion on the web visit https://groups.google.com/d/msg/powermock/-/Slp5UERtNTV4b0VK.

John Rix

unread,
Jun 26, 2014, 9:04:43 AM6/26/14
to powe...@googlegroups.com
I am able to get the @Mock annotation working by adding classes to the PrepareForTest annotation as suggested. However, the RETURNS_DEEP_STUBS answer appears to no longer be effective and I see NullPointerExceptions when creating mock responses for deep stub invocations eg.

        @Mock(answer = Answers.RETURNS_DEEP_STUBS) CloseableHttpResponse mockResponse;

@Before
public void setup() {
                when(mockResponse.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_OK);
        }

Anyone else had this problem?

Thanks,
John

Johan Haleby

unread,
Jun 26, 2014, 9:13:47 AM6/26/14
to powe...@googlegroups.com
Probably a bug. Would be great if someone could provide a pull request.


To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.

To post to this group, send email to powe...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages