gpt4 book ai didi

java - 通过 Espresso 运行单元测试时,由于 'java.lang.ClassNotFoundException' 错误,仪器运行失败

转载 作者:行者123 更新时间:2023-12-01 12:54:40 27 4
gpt4 key购买 nike

我对 Android 开发编码非常陌生。我开发了一款非常基本的应用程序,只需按一下按钮即可进行 MO 调用。

我正在尝试通过浓缩咖啡测试这个应用程序。这是我的代码:

应用名称:电话

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){

}
}

}

list 文件:

<?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>

测试项目:

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());
}
}

list 文件:

<?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>

异常日志:

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.确保 espresso jar 位于 libs 文件夹中

enter image description here

2.确保在类路径中导出

enter image description here

关于java - 通过 Espresso 运行单元测试时,由于 'java.lang.ClassNotFoundException' 错误,仪器运行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23991722/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com