像日常同樣直接建立一個android項目便可。java
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.collection.self.com.espressotest" minSdkVersion 9 targetSdkVersion 23 versionCode 1 versionName "1.0" //-----Espresso中要求添加的部分(必選項) testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } packagingOptions { exclude 'META-INF/maven/com.google.guava/guava/pom.properties' exclude 'META-INF/maven/com.google.guava/guava/pom.xml' } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) // testCompile 'junit:junit:4.12'----這個必定要刪除,不然AndroidJUnit4沒法下載下來 androidTestCompile 'com.android.support:support-annotations:23.1.1' compile 'com.android.support:appcompat-v7:23.1.1' // Android JUnit Runner-----Espresso中要求添加的部分(必選項) androidTestCompile 'com.android.support.test:runner:0.4.1' // JUnit4 Rules-----Espresso中要求添加的部分(必選項) androidTestCompile 'com.android.support.test:rules:0.4.1' // Espresso core-----Espresso中要求添加的部分(必選項) androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' // // Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource // //-----Espresso中要求添加的部分(可選項) // androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1' // // Espresso-web for WebView support-----Espresso中要求添加的部分(可選項) // androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.1' // // Espresso-idling-resource for synchronization with background jobs-----Espresso中要求添加的部分(可選項) // androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:2.2.1' }
package com.collection.self.com.espressotest; 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()+"!"); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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.collection.self.com.espressotest.MainActivity"> <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:hint="Enter your name here" android:id="@+id/editText" android:layout_below="@+id/textView"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Say hello!" android:layout_below="@+id/editText" android:onClick="sayHello"/> </RelativeLayout>
點擊運行按鈕測試程序是否能夠正常運行android
在app/src/androidTest/項目對應的packageName/此目錄中新建一個類。目前用到的類的名字爲:MainActivityInstrumentationTest.javaweb
其中具體的代碼以下:app
package com.collection.self.com.espressotest; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.LargeTest; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; 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.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; /** * Created by jane on 2016/2/19. */ @RunWith(AndroidJUnit4.class) @LargeTest public class MainActivityInstrumentationTest { private static final String STRING_TO_BE_TYPED = "Peter"; @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>( MainActivity.class); @Test public void sayHello(){ onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); //line 1 onView(withText("Say hello!")).perform(click()); //line 2 String expectedText = "Hello, " + STRING_TO_BE_TYPED + "!"; onView(withId(R.id.textView)).check(matches(withText(expectedText))); //line 3 } }
關閉測試機三個選項。具體操做步驟以下:maven
On your device, under Settings->Developer options disable the following 3 settings:ide
Window animation scale測試
Transition animation scalegradle
Animator duration scaleui
在MainActivityInstrumentationTest.java中右鍵點擊,在彈出的菜單中選擇Run MainActivityInstrume。google
以下圖所示:
java.lang.NoClassDefFoundError: com.collection.self.com.espressotest.MainActivity at com.collection.self.com.espressotest.MainActivityInstrumentationTest.<init>(MainActivityInstrumentationTest.java:41) at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:417) at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217) at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1667)
主要緣由是 app/build.gradle配置文件中有多餘的配置選項致使的。因此只須要將不須要的Espresso配置文件刪除掉就能夠了。
最後的結果見上面的build.gradle的配置文件
參考地址: