Android单元测试研究与实践(10)
SampleActivitysampleActivity=Robolectric.buildActivity(SampleActivity.class). create ().resume().get(); assertNotNull(sampleActivity); assertEquals( Activity的标题 ,sampleActivity.getTitle()); }
SampleActivity sampleActivity=Robolectric.buildActivity(SampleActivity.class).
create().resume().get();
assertNotNull(sampleActivity);
assertEquals("Activity的标题", sampleActivity.getTitle());
}
Robolectric.buildActivity()用于构造Activity,create()函数执行后,该Activity会运行到onCreate周期,resume()则对应onResume周期。assertNotNull和assertEquals是JUnit中的断言,Robolectric只提供运行环境,逻辑判断还是需要依赖JUnit中的断言。
原文转自:http://tech.meituan.com/Android_unit_test.html