如何編寫與系統類(即Android Framework類)交互的單元測試?如何對與系統(或Android)類進行交互的單元測試方法進行單元測試
想象一下,你有這些類:
public class DeviceInfo {
public final int screenWidth, screenHeight;
public final String model;
public DeviceInfo(int screenWidth, int screenHeight, String deviceModel) {
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
this.model = deviceModel;
}
}
public class DeviceInfoProvider {
private final Context context;
public DeviceInfoProvider(Context context) {
this.context = context;
}
public DeviceInfo getScreenParams() {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
String model= Build.MODEL;
DeviceInfo params = new DeviceInfo(screenWidth, screenHeight, model);
return params;
}
}
我怎麼能寫一個測試,以驗證該方法DeviceInfoProvider.getScreenParams()
的正確行爲。
下測試通過,但它是非常醜陋的,脆弱的:
@Test
public void testGetScreenParams() throws Exception {
// Setup
Context context = spy(RuntimeEnvironment.application);
DeviceInfoProvider deviceInfoProvider = new DeviceInfoProvider(context);
// Stub
WindowManager mockWindowManager = mock(WindowManager.class);
Display mockDisplay = mock(Display.class);
when(context.getSystemService(Context.WINDOW_SERVICE)).thenReturn(mockWindowManager);
when(mockWindowManager.getDefaultDisplay()).thenReturn(mockDisplay);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
DisplayMetrics metrics = (DisplayMetrics) invocation.getArguments()[0];
metrics.scaledDensity = 3.25f;
metrics.widthPixels = 1081;
metrics.heightPixels = 1921;
return null;
}
}).when(mockDisplay).getMetrics(any(DisplayMetrics.class));
// Run
DeviceInfo deviceInfo = deviceInfoProvider.getScreenParams();
// Verify
assertThat(deviceInfo.screenWidth, equalTo(1081));
assertThat(deviceInfo.screenHeight, equalTo(1921));
assertThat(deviceInfo.model, equalTo(Build.MODEL));
}
您認爲應如何改進呢?
注:目前我使用Robolectric,和的Mockito PowerMock
抽象的上下文特定的代碼,以便它可以被嘲笑單元測試。儘量避免嘲笑你不擁有的課程。 – Nkosi
是'DeviceInfoProvider'的意思是用作依賴? – Nkosi
的問題是:你爲什麼要測試這個?你不相信谷歌的人嗎?你應該首先對你的代碼進行單元測試,並嘲笑你不負責的依賴。 –