单元测试Android活动

时间:2018-06-25 11:59:57

标签: java android unit-testing testing

我正在android studio中制作一个应用程序。对于这个项目,我必须进行多个单元测试。但是我不知道我可以对该应用程序进行单元测试。我真的在单元测试中挣扎,因此,如果有人可以帮助我,或者想出一些我可以在项目中实现的单元测试,我将不胜感激。它是一个具有列表视图的应用程序,可从Web服务检索数据。

代码:

 public class MainActivity extends AppCompatActivity {

ListView listView;
ArrayAdapter<String> adapter;
String[] data;
String[] waarde;
String[] hoog;
String[] laag;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = findViewById(R.id.listview);

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());

    getData();

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(MainActivity.this, Showdata.class);
        intent.putExtra("teeltbed", listView.getItemAtPosition(position).toString());
        startActivity(intent);
        }
    });

}

private void getData()
{
    String getData = null;
    String dbResult = "empty";
    dbConnect database = new dbConnect(this);

    try{
        String query = "SELECT * FROM Lamp";
        getData = "?query=" + URLEncoder.encode(query, "UTF-8");
        //data += "&username=" + URLEncoder.encode(userName, "UTF-8");
        String link = "http://10.247.240.53/kas/lampen.php";
        dbResult = database.execute(link).get();
    }
    catch (Exception e){

    }

    try{
        JSONObject jsonObject = new JSONObject(dbResult);
        JSONArray array = jsonObject.getJSONArray("Lamp");
        waarde = new String[array.length()];
        data = new String[array.length()];
        hoog = new String[array.length()];
        laag = new String[array.length()];
        for (int i = 0; i < array.length(); i++)
        {
            jsonObject = array.getJSONObject(i);
            data[i] = jsonObject.getString("teeltbed");
            waarde[i] = "A: " + jsonObject.getString("waarde") + " %";
            hoog[i] = "H: " + jsonObject.getString("hoog") + " W/m2";
            laag[i] = "L: " + jsonObject.getString("laag") + " W/m2";

        }

        listView.setAdapter(new dataListAdapter(data, waarde, hoog, laag));
    }

    catch (Exception e)
    {
        e.printStackTrace();
    }
}

class dataListAdapter extends BaseAdapter{
    String[] data, waarde;

    dataListAdapter(){
        data = null;
        waarde = null;
    }

    public dataListAdapter(String[] sdata, String[]swaarde, String[]shoog, String[]slaag)
    {
        data = sdata;
        waarde = swaarde;
        hoog = shoog;
        laag = slaag;

    }

    public int getCount()
    {
        return data.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView (int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = getLayoutInflater();
        View row;
        row = inflater.inflate(R.layout.layout_list, parent, false);
        TextView t1, t2, t3, t4;
        t1 = (TextView) row.findViewById(R.id.list_item);
        t2 = (TextView) row.findViewById(R.id.list_item2);
        t3 = (TextView) row.findViewById(R.id.list_item3);
        t4 = (TextView) row.findViewById(R.id.list_item4);
        t1.setText(data[position]);
        t2.setText(waarde[position]);
        t3.setText(hoog[position]);
        t4.setText(laag[position]);
        return (row);
    }
}

class Sproeier
{
 public int TeeltBed;
 public String Stand;
 public double Actueel;
 public double Hoog;
 public double Laag;

 public void Sproeier()
 {
     int teeltbed;
     double hoog;
     double laag;
 }
}

class Lamp {

}

public static class ScadaWebservice
{

}

class Kas
{

    public void kas()
    {

    }
}

}

1 个答案:

答案 0 :(得分:0)

因此,实际上您的应用程序没有很多测试方法,但从我看来,您可以测试:

  • 项目点击监听器的列表视图
  • 如果将getData分成较小的部分,则可以测试是否收到响应或错误以及是否收到数据(如果出现响应)

对于UI测试,您可能需要查看本课程的第8章: https://eu.udacity.com/course/advanced-android-app-development--ud855

或此代码实验室可提供更多建议: https://codelabs.developers.google.com/codelabs/android-testing/index.html?index=..%2F..%2Findex#0