[英]Instrumentation run failed due to 'java.lang.ClassNotFoundException'error while running Unit test through Espresso
I am very new in android development coding. 我是android开发编码中的新手。 I have developed one very basic app for MO call through pressing one button. 我已经开发了一个非常基本的应用程序,只需按一个按钮即可进行MO通话。
I am trying to test this app through espresso.Here is my code : 我正在尝试通过espresso测试此应用。这是我的代码:
app Name : Phonecall 应用名称 :电话
MainActicity.java code : MainActicity.java代码 :
package com.test.phonecall;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public void onClickFun(View view)
{
try
{
Uri number = Uri.parse("tel:" + "xxxxxxxx");
Intent dial = new Intent(Intent.ACTION_CALL, number);
startActivity(dial);
}catch(Exception e){
}
}
}
Manifest File: 清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.phonecall"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.test.phonecall.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Test Project : 测试项目:
package com.test.phonecall.test;
import junit.framework.Assert;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import com.google.android.apps.common.testing.ui.espresso.Espresso;
import com.google.android.apps.common.testing.ui.espresso.action.ViewActions;
import com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers;
import com.test.phonecall.MainActivity;
import com.test.phonecall.R;
public class TestMainActivity extends ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity activity;
private Instrumentation instrumentation;
public TestMainActivity(String name) {
super(MainActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
this.activity = (MainActivity) super.getActivity();
this.instrumentation = super.getInstrumentation();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testPhoneCallBtn() {
try {
Log.d("Test","in testPhoneCallBtn");
Espresso.onView(ViewMatchers.withId(R.id.button1)).perform(ViewActions.click());
} catch (Exception e) {
Log.d("MyTestApp", e.getMessage());
}
}
public void testActivityTitleIsCorrect()
{
Assert.assertTrue(this.activity.getTitle().equals("PhoneCall"));
Log.d("Test","in testPhoneCallBtn");
Espresso.onView(ViewMatchers.withId(R.id.button1)).perform(ViewActions.click());
}
}
Manifest File: 清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.phonecall.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="20" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
<instrumentation
android:name="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
android:targetPackage="com.test.phonecall"/>
</manifest>
Exception Logs : 异常日志:
01-12 06:10:35.569: W/dalvikvm(10800): threadid=1: thread exiting with uncaught exception (group=0x415d9d58)
01-12 06:10:35.569: E/AndroidRuntime(10800): FATAL EXCEPTION: main
01-12 06:10:35.569: E/AndroidRuntime(10800): Process: com.test.phonecall, PID: 10800
01-12 06:10:35.569: E/AndroidRuntime(10800): java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo{com.test.phonecall.test/com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner}: java.lang.ClassNotFoundException: Didn't find class "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.test.phonecall.test-2.apk", zip file "/data/app/com.test.phonecall-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.test.phonecall.test-2, /data/app-lib/com.test.phonecall-2, /vendor/lib, /system/lib]]
01-12 06:10:35.569: E/AndroidRuntime(10800): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4379)
01-12 06:10:35.569: E/AndroidRuntime(10800): at android.app.ActivityThread.access$1500(ActivityThread.java:138)
01-12 06:10:35.569: E/AndroidRuntime(10800): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259)
01-12 06:10:35.569: E/AndroidRuntime(10800): at android.os.Handler.dispatchMessage(Handler.java:102)
01-12 06:10:35.569: E/AndroidRuntime(10800): at android.os.Looper.loop(Looper.java:136)
01-12 06:10:35.569: E/AndroidRuntime(10800): at android.app.ActivityThread.main(ActivityThread.java:5111)
01-12 06:10:35.569: E/AndroidRuntime(10800): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 06:10:35.569: E/AndroidRuntime(10800): at java.lang.reflect.Method.invoke(Method.java:515)
01-12 06:10:35.569: E/AndroidRuntime(10800): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-12 06:10:35.569: E/AndroidRuntime(10800): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
01-12 06:10:35.569: E/AndroidRuntime(10800): at dalvik.system.NativeStart.main(Native Method)
01-12 06:10:35.569: E/AndroidRuntime(10800): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.test.phonecall.test-2.apk", zip file "/data/app/com.test.phonecall-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.test.phonecall.test-2, /data/app-lib/com.test.phonecall-2, /vendor/lib, /system/lib]]
01-12 06:10:35.569: E/AndroidRuntime(10800): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
01-12 06:10:35.569: E/AndroidRuntime(10800): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
01-12 06:10:35.569: E/AndroidRuntime(10800): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
01-12 06:10:35.569: E/AndroidRuntime(10800): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4376)
01-12 06:10:35.569: E/AndroidRuntime(10800): ... 10 more
1.Ensure that the espresso jar is in the libs folder 1.确保意式浓缩咖啡罐在libs文件夹中
2.Ensure that it is exported in the class path 2.确保将其导出到类路径中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.