I am attempting to use Mockito to unit test an Android application, specifically a fragment that gets initialized use newInstance(). I am having trouble injecting my Mock object into a private member variable of the object under test. All the examples I have seen show using new()
instead of newInstance()
and also show the @InjectMocks
annotation on the same line as the initialization. Can I not declare the variable with the annotation but wait to initialize it later?
I have:
public class MyFragmentTest extends TestCase {
@Mock (name="memberVariableName") private MyObject mMyObjectMock;
@InjectMocks private MyFragment mFragment;
@Override
protected void setUp() throws Exception {
super.setUp();
// Initialize mock objects that are being injected
MockitoAnnotations.initMocks(this);
// Work around for a known issue with Mockito and Dexmaker
System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());
}
}