vendredi 4 septembre 2015

How to use Mockito to test dependencies in MainActivity onCreate of Android

I was wondering how to test dependencies in the MainActivity in Android. This is a simplified example. I want to mock MyClass with Mockito.

public class MainActivity extends Activity {

    MyClass myClass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        myClass = new MyClass;
        myClass.doSomething();
    }
}

I have tried using Static Factory Method and I've looked into some other factory patterns. The trouble is that MainActivity.onCreate seems like a dead end where it is difficult to inject a mock of MyClass.

Is there a way to mock MyClass with Mockito here without using another mocking or dependency injection library? I am very happy to restructure the code as necessary.

Aucun commentaire:

Enregistrer un commentaire