MainTest

'界面设计'

………

  <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Hello Word"

        android:id="@+id/textView"

        android:textSize="20dp"/>

    <EditText

        android:hint="Enter your name here"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/editText"

        android:textSize="20dp"

        android:layout_below="@+id/textView"

        android:layout_alignParentRight="true"

        android:layout_alignParentEnd="true" />

    <Button

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Say Hello"

        android:id="@+id^tton"

        android:textSize="50dp"

        android:layout_marginTop="67dp"

        android:layout_below="@+id/editText"

        android:layout_alignParentRight="true"

        android:layout_alignParentEnd="true" />

………

'界面代码'

………

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    private EditText editText;

    private Button button;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);textView= (TextView) findViewById(R.id.textView);

        editText= (EditText) findViewById(R.id.editText);

        button= (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                editText.setText(textView.getText().toString());

            }

        });

    }

………

'代码测试'

………

package com.example.administrator.main;

import android.app.Activity;

import android.test.ActivityTestCase;

import junit.framework.TestCase;

import java.text.RuleBasedCollator;

/**

 * Created by Administrator on 2017/3/16

 */

public class MainActivityTest extends TestCase {

private static final String STRING_TO_BE_TYPED="Peter";

    public ActivityTestCaseRule<MainActivity> mainActivityRule=new ActivityTestRule<>(MainActivity);

    public void sayhello() {

   onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED)).closeSoftKeyboard;

           String expectedText="Hello,"+STRING_TO_BE_TYPED+"!";

        onView(withId(R.Id.textView)).check(matches(withText(expectedText)));

    }

}

原文地址:https://www.cnblogs.com/32zmx/p/6560194.html