???????????Robolectric3.0
???????????????????????????????м?????????????????????????????????????TDD??????д??????????????д????????????????????Χ???????????????????????????????顣
?????????β??????д??????????棬?????????????????????????????????????????????????????????????????????????д?????????????????????????????QA?????????м???????????????????????????????????????????????????
????????Android app?????д??????????????????浥???????????????????????????????鷳???????????????棬?Щ????Android SDK???????Activity??TextView??????????????Robolectric??????????????????????·?????????????JVM?????е?Android???????????????Android???????в?????????Robolectric3.0?????????????о?????ζ?Android????????????????в?????????
??????????????
????Gradle????
??????build.gradle???????????????????
????testCompile "org.robolectric:robolectric:3.0"
??????????????TestRunner
????@RunWith(RobolectricGradleTestRunner.class)
????@Config(constants = BuildConfig.class)
????public class SampleActivityTest {
????}
????Android Studio??????
????1????Build Variants????У???Test Artifact?л???Unit Tests???????????

????
????Test Artifact

????working directory ?????$MODULE_DIR$
????????????????????????????????????????working directory?????
????java.io.FileNotFoundException: buildintermediatesundlesdebugAndroidManifest.xml (????????????·????)
???????÷???????????:

?????????????????ο?????????
????????Activity?????
????1??????Activity???
????@Test
????public void testActivity() {
????SampleActivity sampleActivity = Robolectric.setupActivity(SampleActivity.class);
????assertNotNull(sampleActivity);
????assertEquals(sampleActivity.getTitle()?? "SimpleActivity");
????}
????2??????????
????@Test
????public void testLifecycle() {
????ActivityController<SampleActivity> activityController = Robolectric.buildActivity(SampleActivity.class).create().start();
????Activity activity = activityController.get();
????TextView textview = (TextView) activity.findViewById(R.id.tv_lifecycle_value);
????assertEquals("onCreate"??textview.getText().toString());
????activityController.resume();
????assertEquals("onResume"?? textview.getText().toString());
????activityController.destroy();
????assertEquals("onDestroy"?? textview.getText().toString());
????}
????3?????
????@Test
????public void testStartActivity() {
????//??????????????????Activity
????forwardBtn.performClick();
????Intent expectedIntent = new Intent(sampleActivity?? LoginActivity.class);
????Intent actualIntent = ShadowApplication.getInstance().getNextStartedActivity();
????assertEquals(expectedIntent?? actualIntent);
????}
????4??UI?????
????@Test
????public void testViewState(){
????CheckBox checkBox = (CheckBox) sampleActivity.findViewById(R.id.checkbox);
????Button inverseBtn = (Button) sampleActivity.findViewById(R.id.btn_inverse);
????assertTrue(inverseBtn.isEnabled());
????checkBox.setChecked(true);
????//????????CheckBox???
????inverseBtn.performClick();
????assertTrue(!checkBox.isChecked());
????inverseBtn.performClick();
????assertTrue(checkBox.isChecked());
????}
????5??Dialog
????@Test
????public void testDialog(){
????//????????????????
????dialogBtn.performClick();
????AlertDialog latestAlertDialog = ShadowAlertDialog.getLatestAlertDialog();
????assertNotNull(latestAlertDialog);
????}
????6??Toast
????@Test
????public void testToast(){
????//???????????????
????toastBtn.performClick();
????assertEquals(ShadowToast.getTextOfLatestToast()??"we love UT");
????}
????7??Fragment?????
??????????support??Fragment???????????????
????testCompile "org.robolectric:shadows-support-v4:3.0"
????shadow-support???????Fragment????????Activity?е??????SupportFragmentTestUtil.startFragment()?????????????????
????@Test
????public void testFragment(){
????SampleFragment sampleFragment = new SampleFragment();
????//??api???????????Fragment??Activity?У???????Fragment??onCreateView()
????SupportFragmentTestUtil.startFragment(sampleFragment);
????assertNotNull(sampleFragment.getView());
????}
????8????????????
????@Test
????public void testResources() {
????Application application = RuntimeEnvironment.application;
????String appName = application.getString(R.string.app_name);
????String activityTitle = application.getString(R.string.title_activity_simple);
????assertEquals("LoveUT"?? appName);
????assertEquals("SimpleActivity"??activityTitle);
????}