Android下的单元测试

发表于:2009-11-02 16:36

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:gty509    来源:51Testing软件测试博客

分享:

  2.用ActivityUnitTestCase

  Java代码

   1. public class ForwardingTest extends ActivityUnitTestCase<Forwarding> { 
   2.  
   3.     private Intent mStartIntent; 
   4.     private Button mButton; 
   5.  
   6.     public ForwardingTest() { 
   7.         super(Forwarding.class); 
   8.       } 
   9.  
  10.     @Override 
  11.     protected void setUp() throws Exception { 
  12.         super.setUp(); 
  13.  
  14.         // In setUp, you can create any shared test data, or set up mock components to inject 
  15.         // into your Activity.  But do not call startActivity() until the actual test methods. 
  16.         mStartIntent = new Intent(Intent.ACTION_MAIN); 
  17.     } 
  18.  
  19.     /**
  20.      * The name 'test preconditions' is a convention to signal that if this
  21.      * test doesn't pass, the test case was not set up properly and it might
  22.      * explain any and all failures in other tests.  This is not guaranteed
  23.      * to run before other tests, as junit uses reflection to find the tests.
  24.      */ 
  25.     @MediumTest 
  26.     public void testPreconditions() { 
  27.         startActivity(mStartIntent, null, null); 
  28.         mButton = (Button) getActivity().findViewById(R.id.go); 
  29.          
  30.         assertNotNull(getActivity()); 
  31.         assertNotNull(mButton); 
  32.     } 
  33.      
  34.     /**
  35.      * This test demonstrates examining the way that activity calls startActivity() to launch 
  36.      * other activities.
  37.      */ 
  38.     @MediumTest 
  39.     public void testSubLaunch() { 
  40.         Forwarding activity = startActivity(mStartIntent, null, null); 
  41.         mButton = (Button) activity.findViewById(R.id.go); 
  42.          
  43.         // This test confirms that when you click the button, the activity attempts to open 
  44.         // another activity (by calling startActivity) and close itself (by calling finish()). 
  45.         mButton.performClick(); 
  46.          
  47.         assertNotNull(getStartedActivityIntent()); 
  48.         assertTrue(isFinishCalled()); 
  49.     } 
  50.      
  51.     /**
  52.      * This test demonstrates ways to exercise the Activity's life cycle.
  53.      */ 
  54.     @MediumTest 
  55.     public void testLifeCycleCreate() { 
  56.         Forwarding activity = startActivity(mStartIntent, null, null); 
  57.          
  58.         // At this point, onCreate() has been called, but nothing else 
  59.         // Complete the startup of the activity 
  60.         getInstrumentation().callActivityOnStart(activity); 
  61.         getInstrumentation().callActivityOnResume(activity); 
  62.          
  63.         // At this point you could test for various configuration aspects, or you could  
  64.         // use a Mock Context to confirm that your activity has made certain calls to the system 
  65.         // and set itself up properly. 
  66.          
  67.         getInstrumentation().callActivityOnPause(activity); 
  68.          
  69.         // At this point you could confirm that the activity has paused properly, as if it is 
  70.         // no longer the topmost activity on screen. 
  71.          
  72.         getInstrumentation().callActivityOnStop(activity); 
  73.          
  74.         // At this point, you could confirm that the activity has shut itself down appropriately, 
  75.         // or you could use a Mock Context to confirm that your activity has released any system 
  76.         // resources it should no longer be holding. 
  77.  
  78.         // ActivityUnitTestCase.tearDown(), which is always automatically called, will take care 
  79.         // of calling onDestroy(). 
  80.     } 
  81.  
  82. } 

22/2<12
价值398元的测试课程免费赠送,填问卷领取吧!

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计

法律顾问:上海漕溪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2022
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号