• Android(java)学习笔记108:Android的Junit调试


    1. Android的Junit调试:

    编写android应用的时候,往往我们需要编写一些业务逻辑实现类,但是我们可能不能明确这个业务逻辑是否可以成功实现,特别是逻辑代码体十分巨大的时候,我们不可能一行一行检查自己的代码,为了解决这样的问题就出现了:

       Android下编写单元测试代码-----Junit Test

    测试逻辑是:在Eclipse我们待测试项目中编写测试代码,然后运行测试代码,系统会把代码布署到模拟器或者真机中,代码运行之后,会反馈测试结果给Eclipse,用户就知道业务逻辑类是否可以成功实现。

    首先我们明确Android下编写单元测试代码的步骤

      1)编写测试类,extends AndroidTestCase

      2)编写测试方法, 修饰符是public,直接抛出异常给测试框架 throws Exception,记得让测试方法直接抛出throws异常不能在方法内部try...catch,这是捕获异常,我们这里必须抛出异常,让用户知道.

      3)进行断言

      4)清单文件配置 在application节点配置<uses-library android:name="android.test.runner"/> 在manifest节点里面配置<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="当前应用程序的包名">

      5)运行测试用例。绿条测试通过,红条测试失败.

    2. Android的Junit调试案例:

    (1)首先我们建立一个android项目,命名为:01_测试用例,如下图:

    (2)这里我们还没有对主程序进行编写,我们先编写一个义务逻辑类CalcService.java,放到我们业务实现包com.itheima.junit.service,代码内容如下:

    package com.itheima.junit.service;
    
    /**
     * 计算器的业务类 完成加减乘除的操作
     *
     */
    public class CalcService {
        /**
         * 两个数字相加
         * @param x
         * @param y
         */
        public int add(int x,int y){
            return x+y;
        }
        
    }

    这里就是一个简单实现加法的逻辑方法

    (3)前面我们已经编写好了待测试的业务逻辑类,下面自然就是编写测试代码,这个测试代码我们再新建一个包:com.itheima.junit.test包中,这个测试类我们命名为

    TestCalService.java,继承自AndroidTestCase这里这样命名就会见名知意,很明显知道这是测试CalService用途的。测试代码内容如下:

    package com.itheima.junit.test;
    
    import com.itheima.junit.service.CalcService;
    
    import android.test.AndroidTestCase;
    /**
     * 1. android下的测试代码 一定记得继承一个父类AndroidTestCase
     */
    public class TestCalcService extends AndroidTestCase{
        
        /**
         * 2.编写测试方法记得 说有的测试方法 都应该是public
         * 记得让测试方法直接抛出异常,不能在方法内部try...catch,这是捕获异常,我们这里必须抛出异常,让用户知道
         * 根据需求进行断言操作 assert
         */
        public void testAdd() throws Exception{
            CalcService calcService = new CalcService();
            int result = calcService.add(3, 5);
            assertEquals(8, result);
        }
    }

    (4)配置AndroidManifest.xml清单文件如下:

    <?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)进入J2EE模式,如下:

    运行之后,等待系统会布署代码到模拟器或真机上,在模拟器或真机上面运行成功之后,会反馈测试结果该Eclipse,报错的会也会告知Eclipse,成功了也会告知Eclipse。

  • 相关阅读:
    Flask + WSGI + Nginx 环境
    sql字段合并与分组聚合
    杭州优科豪马轮胎有限公司北京经销商
    国家与大洲对应关系json数据
    【C#】编码史记
    【C#】Unicode的流言终结者和编码大揭秘
    【WPF】生成二维码
    【WPF】WriteableBitmap和BitmapImage的相互转换
    TPL之延续任务
    【C#】日期格式转换
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4734444.html
Copyright © 2020-2023  润新知