我试图为Android应用程序编写一个黑盒子测试,其中我只使用了Robotium的APK(无源代码)。 我正在使用android studio 3.1.2。 这里是代码片段,
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import com.robotium.solo.Solo;
@SuppressWarnings("rawtypes")
public class RobotiumTest extends ActivityInstrumentationTestCase2 {
private static final String
LAUNCHER_ACTIVITY_FULL_CLASSNAME
= "com.example.hqsg.activities.HQSoundGuideActivity";
private static Class launcherActivityClass;
static {
try {
launcherActivityClass = Class
.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public RobotiumTest() throws ClassNotFoundException {
super(launcherActivityClass);
}
private Solo mDevice;
@Override
public void setUp() throws Exception {
mDevice = new Solo(getInstrumentation(), getActivity());
super.setUp();
}
@Override
public void tearDown() throws Exception {
mDevice.finishOpenedActivities();
super.tearDown();
}
public void test() {
mDevice.clickOnButton(0);
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.robotium">
<uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-library android:name="android.test.runner" />
</application>
</manifest>
Gradle文件
apply plugin: 'com.android.application'
android {
signingConfigs {
config {
keyAlias 'androiddebugkey'
keyPassword 'android'
storeFile file('C:/Users/suman_A/Desktop/key.jks')
storePassword 'android'
}
}
compileSdkVersion 27
defaultConfig {
applicationId "com.example.robotium"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testApplicationId 'com.example.hqsg'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
signingConfig signingConfigs.config
multiDexEnabled true
}
buildTypes {`enter code here`
release {
minifyEnabled false
enter code here proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
signingConfig signingConfigs.config
}
}
testOptions {
unitTests.returnDefaultValues = true
}
buildToolsVersion '27.0.3'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
debugImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support:support-annotations:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
implementation 'com.jayway.android.robotium:robotium:5.6.1'
androidTestImplementation 'com.jayway.android.robotium:robotium-solo:5.6.1'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.android.support:multidex:1.0.3'
}
执行应用程序后,我面临InitializerError。 这是错误报告
java.lang.ExceptionInInitializerError
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at junit.framework.TestSuite.createTest(TestSuite.java:61)
at junit.framework.TestSuite.addTestMethod(TestSuite.java:294)
at junit.framework.TestSuite.addTestsFromTestCase(TestSuite.java:150)
at junit.framework.TestSuite.<init>(TestSuite.java:129)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite.<init>(NonLeakyTestSuite.java:33)
at android.support.test.internal.runner.junit3.AndroidTestSuite.<init>(AndroidTestSuite.java:46)
at android.support.test.internal.runner.junit3.AndroidJUnit3Builder.runnerForClass(AndroidJUnit3Builder.java:87)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at android.support.test.internal.runner.AndroidRunnerBuilder.runnerForClass(AndroidRunnerBuilder.java:153)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at android.support.test.internal.runner.TestLoader.doCreateRunner(TestLoader.java:73)
at android.support.test.internal.runner.TestLoader.getRunnersFor(TestLoader.java:104)
at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:789)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:539)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:382)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.example.hqsg.activities.HQSoundGuideActivity
at com.example.robotium.RobotiumTest.<clinit>(RobotiumTest.java:25)
... 19 more
Caused by: java.lang.ClassNotFoundException: com.example.hqsg.activities.HQSoundGuideActivity
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:400)
at java.lang.Class.forName(Class.java:326)
at com.example.robotium.RobotiumTest.<clinit>(RobotiumTest.java:20)
... 19 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.hqsg.activities.HQSoundGuideActivity" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.example.hqsg-1/base.apk", zip file "/data/app/com.example.robotium-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.hqsg-1/lib/arm, /data/app/com.example.robotium-1/lib/arm, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 23 more
测试完成。 有谁知道这个?