I have a test class with a "conventional" arrangement of several @Mock-ed instance variables, and one @InjectMocks-ed instance of the class under test.
In at least one test method, I need to instead create a spied instance of the CUT because of some annoying methods in the proprietary base class. Before I had @InjectMocks, I had to manually wire up all the mocked instances into my CUT instance. It appears I have to do this again, for a spied instance, because I don't know how to manually do what @InjectMocks does. Is it possible to just call something at this point in the test method to do with my spy what @InjectMocks would have done with my normal CUT?
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
I’m afraid that didn’t appear to do anything. To illustrate, my CUT class is named Bar, which has a property of type Foo that I need to inject a mocked instance of.
I did something like the following:
Bar bar = new Bar();
Bar spiedBar = spy(bar);
doReturn(...).when(spiedBar)...
MockitoAnnotations.initMocks(bar);
spiedBar.methodcall();
When the last line was executed, I hit a NPE in the CUT when it referenced an instance variable that should have been mocked.
Concerning “magic”, it’s only magic until it becomes second nature and commonly used, just like every enhancement to humankind developed over the years that was considered “magic” when it was first developed, and which we couldn’t live without now.
Just so it’s clear, the expected workaround of calling a “wireCUT()” method that manually calls the setter methods on the CUT, passing in the mocked instances, did do what I expected, which is what I was hoping “MockitoAnnotations.initMocks()” would do. I had to pass “wireCUT()” the spy, not the spied instance, in order for this to work. I’ll use this unless I can find a cleaner and perhaps more “magical” :) way.