Katarzynasrom / powermock

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

Support PowerMock Annotations Equivalent to @Mock #331

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I much prefer the readability of the annotated style of Mockito declarations 
like: @Mock Editable editable;

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

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);

Even better would be if you could somehow reuse the @Mock annotation and behave 
correctly regardless of the class implementation (final or not.)

Original issue reported on code.google.com by glenview...@gmail.com on 6 Jun 2011 at 2:30

GoogleCodeExporter commented 8 years ago
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.  If nothing else, maybe this 
pitfall could be warned against 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

Original comment by glenview...@gmail.com on 7 Jun 2011 at 3:12

GoogleCodeExporter commented 8 years ago
It cannot be deprecated since MockitoAnnotations is Mockito code. But it could 
be added to the documentation.

Original comment by johan.ha...@gmail.com on 7 Jun 2011 at 5:53

GoogleCodeExporter commented 8 years ago
This could go too far; I'm not even certain I would do it, but you could add an 
initMocks() method to PowerMockito and mark it as deprecated.  Although this 
really wouldn't have saved someone in my shoes, since the dependency would have 
presumably already been satisfied by Mockito.

Alternately and much preferably, if you would contribute whatever technique you 
are using to eliminate the initMocks() call over to Mockito itself, I think it 
would make Mockito cleaner as well.

Thanks for updating the docs!

Original comment by glenview...@gmail.com on 7 Jun 2011 at 1:31

GoogleCodeExporter commented 8 years ago
I don't think the first suggestions is a very good idea, I think it would be 
confusing.

The second suggestion will not work very well either. Mockito already has 
another way to inject mocks without using the initMocks() method (you can use 
the MockitoJUnitRunner). If Mockito were to remove the initMocks() method you 
couldn't run Mockito tests with a non-standard JUnit runner. What Mockito could 
do is to create a JUnit Rule that injects the mocks but that will require a 
JUnit version >= 4.7.

Thanks for your suggestions though! Always helpful.

Original comment by johan.ha...@gmail.com on 9 Jun 2011 at 10:19

GoogleCodeExporter commented 8 years ago
Thanks for considering the suggestions; I completely agree with your 
responses/rejection.  Since what you're saying is that equivalent behavior 
seems to already exist in Mockito to avoid the initMocks() method, I'll double 
check the Mockito documentation and ask them to more explicitly document the 
MockitoJUnitRunner option.

Thanks,
Jeff

Original comment by glenview...@gmail.com on 9 Jun 2011 at 1:48