Android Test Tutorial: Servicetestcase sample

Source: Internet
Author: User

Servicetestcase provides a controllable test environment for the test service, provides basic support for the service lifecycle, and can control the test environment by injecting some dependent objects to test the service.

The Servicetestcase class inherits the following illustration:

Service lifecycle support, each service runs in a certain order (lifecycle approach), Servicetestcase provides the following methods to support testing of service lifecycle methods:

The Setup method is executed first before each test method invocation, and the basic implementation of Setup is to get the system context, and if you want to overload setup, pay attention to the first line plus super.setup.

After invoking StartService (Intent) or Bindservice (Intent), Servicetestcase invokes the service's OnCreate method so that You have the opportunity to make some adjustments to the test environment before the service starts.

When your test method calls StartService (Intent) or Bindservice (Intent), Servicetestcase invokes the service's OnCreate method, and then invokes the service's corresponding StartService (Intent) or service Bindservice (Intent, serviceconnection, int) methods. and save the corresponding values for tracking and support lifecycle.

After each test method ends, call the Teardown method, which stops and destroy the service being tested. If you need to overload the teardown, notice to call Super.teardown first.

Dependency injection Each service relies on the context object and application object that runs it, servicetestcase the test framework allows you to inject these objects (modified, mocked, etc.) to achieve real unit testing.

The code for Localservicetest is as follows:

public class 

Localservicetest      
 extends servicetestcase<localservice> {public      

 localservicetest () {
 super (Localservice.class);
 }

 @Override 
 protected void SetUp () throws Exception {
 super.setup ();
 }

 @SmallTest public 
 void Testpreconditions () {
 }

 /**     
 * Test basic startup/shutdown of Service     
 * / 
 @SmallTest public 
 void Teststartable () {      
 Intent startintent = new Intent ();      
 Startintent.setclass (GetContext (), localservice.class);      
 StartService (startintent);      
 }      

 /**     
 * Test binding to service 
 /@MediumTest public 
 void Testbindable () {      
 Intent startintent = New Intent ();      
 Startintent.setclass (GetContext (), localservice.class);      
 IBinder service = Bindservice (startintent);      
 }      

Teststartable test the corresponding service can start normally.

Testbindable test if the corresponding service can bind successfully

See a full set of tutorials: http://www.bianceng.cn/OS/extra/201301/35252.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.