Mock creation of Object inside method
<h3>Problem Description</h3>

<p>I'm trying to mock object creation inside of method.
I have <code>LoginFragment</code> which is creating <code>LoginPresenterImpl</code> inside of <code>onCreate</code> method, like shown below:</p>

<pre><code>public class LoginFragment extends BaseFragment {
    private LoginPresenter mPresenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mPresenter = new LoginPresenterImpl(this); &lt;&lt;-- Should be mocked
    }

}
</code></pre>

<p>I have some problems with combining <code>RobolectricGradleTestRunner</code> and <code>PowerMockRunner</code> in one test but after reading <a href="https://github.com/robolectric/robolectric/wiki/Using-PowerMock" rel="noreferrer">this</a> post, I found way how to do that, so my test look like this:</p>

<h2>BaseRobolectricTest.java</h2>

<pre><code>@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
public abstract class BaseRobolectricTest {

}
</code></pre>

<h2>Test.java</h2>

<pre><code>@PrepareForTest({LoginPresenterImpl.class})
public class Test extends BaseRobolectricTest  {

    private LoginPresenterImpl mPresenterMock;

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Before
    public void setup() {
        mockStatic(LoginPresenterImpl.class);

        mPresenterMock = PowerMockito.mock(LoginPresenterImpl.class);
    }

    @Test
    public void testing() throws Exception {
        when(mPresenterMock.loadUsername(any(Context.class))).thenReturn(VALID_USERNAME);
        when(mPresenterMock.loadPassword(any(Context.class))).thenReturn(VALID_PASSWORD);
        when(mPresenterMock.canAutologin(VALID_USERNAME, VALID_PASSWORD)).thenReturn(true);

        whenNew(LoginPresenterImpl.class).withAnyArguments().thenReturn(mPresenterMock);

        FragmentTestUtil.startFragment(createLoginFragment());
    }

    private LoginFragment createLoginFragment() {
        LoginFragment loginFragment = LoginFragment.newInstance();
        return loginFragment;
    }
}
</code></pre>