Write unit test Code (JUNIT test) under Android (Java) Learning Note 165:android

Source: Internet
Author: User



When writing Android applications, we often need to write some business logic implementation classes, but we may not be able to determine whether this business logic can be successfully implemented, especially when the logic code is very large, we can not check our own code in one line, in order to solve this problem arises:



Write unit test code under Android-----Junit Test



The test logic is: In the eclipse we are testing the project to write the test code, and then run the test code, the system will put the code into the simulator or the real machine, after the code runs, the test results will be fed back to eclipse, the user knows whether the business logic class can be successfully implemented.






First we make clear the steps for writing unit test code under Android:


    1. Write test class, extends Androidtestcase
    2. write test methods, modifiers are public, throw exceptions directly to the test framework throws Exception, Remember to let the test method directly throw throws exception , not inside the method Try...catch, this is catch exception, we must throw an exception here, let the user know
    3. Make assertions
    4. The manifest file configuration is configured on the application node <uses-library android:name= "Android.test.runner"/> in manifest Node inside configuration <instrumentation android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= "the package name of the current application ">
    5. Run the test case. The green bar test passed and the Red bar test failed.





Case:



1. First we build an Android project named: 01_ test Cases, such as:








2. Here we have not written the main program, we first write an obligation logic class Calcservice.java, put into our business implementation package Com.itheima.junit.service, the code content is as follows:


package com.itheima.junit.service;
* *
*Business class of calculator completes the operation of addition, subtraction, multiplication and division
*
* /
public class CalcService {
* *
*Add two numbers
* @param x
* @param y
* /
public int add(int x,int y){
Return x+y;
}
} 


Here is a simple logical way to implement addition






3. Before we have written the business logic class to be tested, it is natural to write the test code , this test code we create a new package:com.itheima.junit.test Package, this test class we named



Test Calservice.java, this is the name here to meet the names, it is clear that this is testing calservice use. The test code reads as follows:


package com.itheima.junit.test;
import com.itheima.junit.service.CalcService;
import android.test.AndroidTestCase;
* *
*1. The test code under Android must inherit a parent class, androidtestcase
* /
public class TestCalcService extends AndroidTestCase{
* *
*2. Write test methods. Remember to say that some test methods should be public
*Remember to ask the test method to throw an exception directly. You can't try... Catch inside the method. This is to catch an exception. We must throw an exception here to let the user know
*Assert operation as required
* /
public void testAdd() throws Exception{
CalcService calcService = new CalcService();
int result = calcService.add(3, 5);
assertEquals(8, result);
}
} 





4. Configure the Androidmanifest.xml manifest file as follows:


 
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.junit"
    android:versionCode="1"
    android:versionName="1.0" >
    <instrumentation android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.itheima.junit"></instrumentation>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="android.test.runner"/>
        <activity
            android:name="com.itheima.junit.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>








5. Enter the Java EE mode as follows:






After running, waiting for the system will be the code to the simulator or the real machine, after the simulator or the real machine run successfully, will be feedback test results of the eclipse, the error will also inform Eclipse, success will also inform Eclipse.



Android (Java) Learning Note 165:android Writing Unit test code (JUNIT test)


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.