android_lesson_2

android单元测试

1.首先在AndroidManifest.xml中加入下面红色代码

  <uses-library android:name="android.test.runner" /> <!-- append to application noe-->

  <!-- append to manifest;  com.example.myfirstapp == manifest.package -->
  <instrumentation android:name="android.test.InstrumentationTestRunner"  android:targetPackage="com.example.myfirstapp" />

2.l编写单元测试代码(选择要测试的方法,右键点击“Run As”--“Android Junit Test” )
  public class InputActivityTest extends AndroidTestCase{
       public void test() throws Exception {
          InputService inputService=new InputService();
          Integer r=inputService.queryPort();
          Assert.assertEquals(new Integer(1), r);
      }
  }

note:
          无法导出文件:"Failed to pull selection",查看文件名是否存在中文、权限是否为rw

<!--申请操作sd卡读和删权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 申请操作sd卡写的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
文件操作:
 
package com.example.myfirstapp;



import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;



import android.content.Context;

import android.os.Environment;



public class FileOperatorService {

    private Context context;



    public void SetContext(Context context) {

        this.context = context;

    }



    public void write(String fileName, String content) throws Exception {

        FileOutputStream out = context.openFileOutput(fileName,context.MODE_PRIVATE);

        out.write(content.getBytes());

        out.close();

    }



    public String read(String fileName) throws Exception {

        FileInputStream reader = context.openFileInput(fileName);

        ByteArrayOutputStream memoryArr = new ByteArrayOutputStream();

        byte[] buf = new byte[1024];

        int len;

        while ((len = reader.read(buf)) != -1) {

            memoryArr.write(buf, 0, len);

        }

        byte[] data = memoryArr.toByteArray();

        memoryArr.close();

        reader.close();

        return new String(data);

    }

    

    public void write2sdCard(String fileName, String content) throws Exception {

        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

            File sdCardDir=Environment.getExternalStorageDirectory();

            File file=new File(sdCardDir,fileName);

            FileOutputStream out=new FileOutputStream(file);

            out.write(content.getBytes());

            out.close();

        }

    }



    public String readFromSDCard(String fileName) throws Exception {

        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

            File sdCardDir=Environment.getExternalStorageDirectory();

            File file=new File(sdCardDir,fileName);

            FileInputStream reader = new FileInputStream(file);

            ByteArrayOutputStream memoryArr = new ByteArrayOutputStream();

            byte[] buf = new byte[1024];

            int len;

            while ((len = reader.read(buf)) != -1) {

                memoryArr.write(buf, 0, len);

            }

            byte[] data = memoryArr.toByteArray();

            memoryArr.close();

            reader.close();

            return new String(data);

        }

        return null;

    }



}

 

你可能感兴趣的:(android)