1.环境配置
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.mrchen.hello"
minSdkVersion 23
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
testCompile 'junit:junit:4.12'
}
2.界面布局
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
tools:layout_editor_absoluteY="16dp"
tools:layout_editor_absoluteX="0dp" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="33dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAY HELLO"
android:onClick="sayHello"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="84dp" />
JAVA代码:
package com.example.mrchen.hello;
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 btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sayHello(View view){
TextView textView = (TextView) findViewById(R.id.textview);
EditText editText = (EditText) findViewById(R.id.editText);
textView.setText("Hello"+editText.getText().toString()+"!");
}
}
测试:
package com.example.mrchen.hello;
import android.support.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.junit.Assert.*;
/**
-
Created by Mr.chen on 2017/3/21.
*/
public class MainActivityTest {
private static final String STRING_TO_BE_TYPED = "Peter";
@Rule
public ActivityTestRulemActivityRule = new ActivityTestRule (MainActivity.class);
@Test
public void sayHello() throws Exception {
onView(withId(R.id.editText)).perform(replaceText(STRING_TO_BE_TYPED),closeSoftKeyboard());onView(withId(R.id.button)).perform(click()); String expectedText="Hello"+STRING_TO_BE_TYPED+"!"; onView(withId(R.id.textview)).check(matches(withText(expectedText)));
}
}
截图: