zoukankan      html  css  js  c++  java
  • Android Studio中进行单元测试 31

    以下为activity_mian代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.gxd.hellowordhw.MainActivity"
    android:orientation="vertical">

    <TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

    <EditText

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText" />

    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="SAY HELLO!"
    android:id="@+id/button"
    android:layout_gravity="center_horizontal" />

    </LinearLayout>


    以下为Mainactivity代码:

    package com.example.gxd.hellowordhw;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

    @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()+"!");
    }
    }

    以下为MainactivityText代码:
    public class MainActivityTest {
         private static final String STRING_TO_BE_TYPEDTO = "Peter";
        @Rule
        public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

        @Test
        public void testSayHello() throws Exception {

            onView(withId(R.id.editText)).perform(TestRule(STRING_TO_BE_TYPEDTO),closeSoftKeyboard);
            onView(withId(R.id.button)).perform(click());
            String expectedText="Hello,"+ STRING_TO_BE_TYPEDTO +"!";
            onView(withId(R.id.textview)).check(matches(withText(expectedText)));


        }


    }

    以下为build.gradle代码:
    apply plugin: 'com.android.application'

    android {
    compileSdkVersion 23
    buildToolsVersion "24.0.2"

    defaultConfig {
    applicationId "com.example.gxd.hellowordhw"
    minSdkVersion 19
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    //add for test
    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'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    //add for test
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    }
    }
    布局:

    
    
  • 相关阅读:
    继承LIst 的类JSON序列化,无法序列化属性的问题
    C#深入学习:泛型修饰符in,out、逆变委托类型和协变委托类型
    12.Java web--过滤器与监听器
    11.Java web—servlet
    10.Java web—JavaBean
    9.Java web—JSP内置对象
    8.Java web—JSP基本语法
    Ubuntu 插入鼠标自动禁用触控板
    Ubuntu安装VLC播放器
    Ubuntu快捷键
  • 原文地址:https://www.cnblogs.com/gudaping/p/6641022.html
Copyright © 2011-2022 走看看