Android JUnit test Environment setup

Source: Internet
Author: User

A Introduction to Unit Testing

In the process of Android development, often need to test the business aspects of code, familiar with Java people know JUnit, in Android Google has been based on JUnit optimized for Android to the automation framework JUnit, In JUnit you can get components that can simulate the correctness of sending events and instrumentation processing, using JUnit to write test cases in Java, we need to inherit testcase, and we need to inherit androidtestcase in Android.

Two Environment construction

1. Business Class

The Add function is written in the business class, and JUnit tests the correctness of the add function.

<span style= "Font-family:times New Roman;" >package Com.mxy.service;public class Calcservice {public int add (int a,int b) {System.out.println ("Execute Add Method"); return a + b;}} </span>

2. Test class

The test class inherits the Androidtestcase class, writes the Testadd function, notes that the function in the test class must start with test, the return value is void, and the type is public.

<span style= "Font-family:times New Roman;" >package Com.mxy.test;import Com.mxy.service.calcservice;import Android.test.androidtestcase;public class Calcservicetest extends Androidtestcase {public void Testadd () {Calcservice cal = new Calcservice (); int result = Cal.add (5 , 3); SYSTEM.OUT.PRINTLN (result); Assertequals (8, result);}} </span>

3. Configure Androidmanifeset.xml

In the <applicetion/> tab, add:

<span style= "Font-family:times New Roman;" ><uses-library android:name= "Android.test.runner"/></span>

In the <mainfest/> tab, add:

<span style= "Font-family:times New Roman;" ><instrumentation android:name= "Android.test.InstrumentationTestRunner"            android:targetpackage= " Com.uicc.cloudservice "android:label=" Test for my App "/></span>

The package name of the Android:targetpackage must be the same as that of the manifest tag.

Three Problems and Solutions

1. Error: Warning:no instrumentation runner found for the launch, using Android.test.InstrumentationTestRunner.

Cause: The emulator cannot remember the configuration of the androidmanifest and will need to reset the run configuration at run time.

The steps to resolve are as follows:

(1). Right-click on the project or test class name to select Properties
(2). In Run/debug setting Select the project name to run, click Edit on the right to enter the edit launch configuration Properties interface, in the Test tab instrumentation Select Android.test.InstrumentationTestRunner from the drop-down list in the runner back.

(3). Rerun the test unit.

2. The package name of the Android:targetpackage must be the same as that of the manifest label. Otherwise, there will be an error that the package could not be found.

Four Reference URL

1. http://blog.csdn.net/mengxiangyue/article/details/20568433 Environment Construction

2. http://blog.csdn.net/you12345678901234567/article/details/7632807 Error Solution

Android JUnit test Environment setup

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.