2018-08-15 10:42:08 1492瀏覽
今天扣丁學(xué)堂Android培訓(xùn)老師給大家介紹一下關(guān)于關(guān)于在Android原生應(yīng)用開(kāi)發(fā)中,存在兩種單元測(cè)試:本地JVM測(cè)試和Instrumentation測(cè)試,那么下面我們來(lái)看一下關(guān)于如何對(duì)Android原生應(yīng)用進(jìn)行單元測(cè)試及示例代碼。3、準(zhǔn)備一個(gè)簡(jiǎn)單的APP,模擬一個(gè)耗時(shí)的網(wǎng)絡(luò)請(qǐng)求獲得一段數(shù)據(jù)并顯示在界面上,針對(duì)這個(gè)APP編寫(xiě)單元測(cè)試用例并進(jìn)行本地單元測(cè)試。
依賴(lài)庫(kù)
testOptions { unitTests { includeAndroidResources true } }
testImplementation 'junit:junit:4.12' testImplementation 'org.robolectric:robolectric:3.8' testImplementation 'org.robolectric:shadows-supportv4:3.8' testImplementation 'org.powermock:powermock-module-junit4:1.6.6' testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.6' testImplementation 'org.powermock:powermock-api-mockito:1.6.6' testImplementation 'org.powermock:powermock-classloading-xstream:1.6.6' testImplementation 'org.mockito:mockito-all:1.10.19'
@RunWith(RobolectricTestRunner.class) @Config(sdk = 21, constants = BuildConfig.class) @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) @PrepareForTest({PresenterFactory.class})
@Before public void setUp() { appContext = RuntimeEnvironment.application.getApplicationContext(); PowerMockito.mockStatic(PresenterFactory.class); }
@Test public void onCreate_text1() { MainActivity activity = Robolectric.buildActivity(MainActivity.class).create().get(); String expect = appContext.getString(R.string.hell_world); assertEquals(expect, ((TextView)activity.findViewById(R.id.lbl_text1)).getText()); }
@Test public void btn1_click() { MainActivity activity = Robolectric.setupActivity(MainActivity.class); activity.findViewById(R.id.btn_1).performClick(); String expect = appContext.getString(R.string.hell_world); assertEquals(expect, ShadowToast.getTextOfLatestToast()); }
@Test public void btn2_click() { MainContract.Presenter presenter = Mockito.mock(MainContract.Presenter.class); PowerMockito.when(PresenterFactory.create(Mockito.any(MainContract.View.class), Mockito.any(AppExecutors.class))) .thenReturn(presenter); MainActivity activity = Robolectric.setupActivity(MainActivity.class); activity.findViewById(R.id.btn_2).performClick(); Mockito.verify(presenter, Mockito.times(1)) .fetch(); }
@RunWith(PowerMockRunner.class) @PrepareForTest({ServiceFactory.class})
@Before public void setUp() { PowerMockito.mockStatic(ServiceFactory.class); }
@Test public void fetch_success() { String expected = "hello world"; SlowService service = Mockito.mock(SlowService.class); Mockito.when(service.fetch()).thenReturn(expected); PowerMockito.when(ServiceFactory.create()) .thenReturn(service); MainContract.View view = Mockito.mock(MainContract.View.class); MainPresenter presenter = new MainPresenter(view, executors); presenter.fetch(); Mockito.verify(service, Mockito.times(1)).fetch(); Mockito.verify(view, Mockito.times(1)).onFetchStarted(); Mockito.verify(view, Mockito.times(1)).onFetchCompleted(); Mockito.verify(view, Mockito.times(0)).onFetchFailed(Mockito.anyObject()); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); Mockito.verify(view, Mockito.times(1)).onFetchSuccess(captor.capture()); assertEquals(expected, captor.getValue()); }
@Test public void fetch_failed() { RuntimeException exception = new RuntimeException("fetch failed"); SlowService service = Mockito.mock(SlowService.class); Mockito.when(service.fetch()).thenThrow(exception); PowerMockito.when(ServiceFactory.create()) .thenReturn(service); MainContract.View view = Mockito.mock(MainContract.View.class); MainPresenter presenter = new MainPresenter(view, executors); presenter.fetch(); Mockito.verify(service, Mockito.times(1)).fetch(); Mockito.verify(view, Mockito.times(1)).onFetchStarted(); Mockito.verify(view, Mockito.times(1)).onFetchCompleted(); ArgumentCaptor<Throwable> captor = ArgumentCaptor.forClass(Throwable.class); Mockito.verify(view, Mockito.times(1)).onFetchFailed(captor.capture()); assertEquals(exception, captor.getValue()); Mockito.verify(view, Mockito.times(0)).onFetchSuccess(Mockito.anyString()); }
public class SlowServiceImplTest { @Test public void fetch_data() { SlowServiceImpl impl = new SlowServiceImpl(); String data = impl.fetch(); assertEquals("from slow service", data); } }
以上就是關(guān)于扣丁學(xué)堂Android開(kāi)發(fā)培訓(xùn)單元測(cè)試最佳實(shí)踐的詳細(xì)介紹,希望對(duì)同學(xué)們學(xué)習(xí)有所幫助,想要了解更多詳情請(qǐng)登錄扣丁學(xué)堂官網(wǎng)咨詢或者關(guān)注微信公眾號(hào),里面有最新的扣丁學(xué)堂Android視頻教程等你來(lái)看!
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“Android開(kāi)發(fā)技術(shù)”的相關(guān)資訊>>