mercredi 6 avril 2016

Robolectric Override Context Values

Problem Description

I'm writing test which are dealing with context and resource functionality, for example I have method which returns screen width or height.

public int getScreenWidth(final Context context) {
   final DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
   return displayMetrics.widthPixels;
}

When while testing I am passing to my method as an argument RuntimeEnvironment.application.getApplicationContext(), my function return 480 for display width.

Also some methods are dealing with Build class like one shown below:

public String getDeviceType() {
    return String.format("%s %s", Build.MANUFACTURER, Build.MODEL);
}

Question

How I can override (change) values which I am getting from Context or Build class, so I can write tests and expect correct values which were pre set by me.

Aucun commentaire:

Enregistrer un commentaire