单元测试Android Studio Textview

时间:2020-03-04 16:14:17

标签: android unit-testing

这是我的主要活动,该活动从URL获取json数组。我的问题是,当我尝试进行单元测试时,textview中应该包含什么内容,这使我得到了空指针。

公共类MainActivity扩展了AppCompatActivity {

TextView txtJson;
ProgressDialog pd;
public static TextView testString;
String jsonString = null;
List<Location> locations;`enter code here`


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

    txtJson = (TextView) findViewById(R.id.tvJsonItem);
    testString = (TextView) findViewById(R.id.test_for_string);

    new JsonTask().execute("https://wsu-dining-service.s3.amazonaws.com/current-menu.json");
}

protected void postCreate()
{
    mapStrinToClass();
    testString.setText(locations.get(0).getName());
}


private void mapStrinToClass()
{
    ObjectMapper objectMapper = new ObjectMapper();
    JsonFactory jsonFactory = objectMapper.getFactory();
    try {
        JsonParser jsonParser = jsonFactory.createParser(jsonString);
        locations = objectMapper.readValue(jsonString,
                new TypeReference<List<Location>>() {
                });
    } catch (IOException e) {
        e.printStackTrace();
    }

}


private class JsonTask extends AsyncTask<String, String, String> {

    protected void onPreExecute() {
        super.onPreExecute();

        pd = new ProgressDialog(MainActivity.this);
        pd.setMessage("Please wait");
        pd.setCancelable(false);
        pd.show();
    }

    protected String doInBackground(String... params) {


        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();


            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();
            String line = "";

            while ((line = reader.readLine()) != null) {
                buffer.append(line+"\n");
                Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)

            }

            return buffer.toString();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (pd.isShowing()){
            pd.dismiss();
        }
        jsonString = result;
        postCreate();
    }
}

}

我的单元测试 *当我运行该应用程序时,textview中会显示“ Tim&Jeanne's Dining Commons”,但测试失败并显示testString.getText()。toString();为空

@Test
public void isMenuCorrect() {
    String menuTxt = MainActivity.testString.getText().toString();
    assert(menuTxt == "Tim & Jeanne's Dining Commons");

}

1 个答案:

答案 0 :(得分:0)

首先,您应该使用Espresso在androidTest文件夹下运行UI测试。示例:

onView(allOf(withId(R.id.tvJsonItem), withText("Tim & Jeanne's Dining Commons")).check(matches(isDisplayed()));

基本上,我们在这里正在检查是否在屏幕上显示ID为R.id.tvJsonItem并带有文本“ Tim&Jeanne's Dining Commons”的视图。现在,如何运行Espresso测试不在此问题的范围内。

第二,您的生产代码永远都不应知道测试中发生了什么,就像您创建了TextView只是用于单元测试中一样。

最后,永远不要对视图有静态引用,因为您不能保证在尝试访问视图时已经创建了活动。实际上,视图只能由其父视图查看。在您的情况下,参考TextView在您的活动中应该是私有的。

相关问题