Android问题:Test run failed: Instrumentation run failed due to 'java.lang.RuntimeException'

在初学Android单元测试时遇到'Instrumentation run failed due to 'java.lang.RuntimeException''的问题。通过检查源码发现,问题出在测试类的构造函数上。修复方法是移除泛型并使用无参数构造函数,然后在构造函数内部调用super(),传入应用包名和Activity类。调整后的构造函数为:super(""hkrt.quickpass.MainActivity"

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初学Android手机单元测试,遇到了很多问题,其中好多问题都在网上搜索解决了,,但这个问题Test run failed: Instrumentation run failed due to 'java.lang.RuntimeException'  没有解决

 

源码如下:

package hkrt.quickpass.test;


import static org.junit.Assert.*;
import hkrt.quickpass.MainActivity;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import android.test.ActivityInstrumentationTestCase2;

public class MainAcvitityTest extends ActivityInstrumentationTestCase2<MainActivity> {

 public MainAcvitityTest(Class<MainActivity> activityClass) {
  super(activityClass);
  // TODO Auto-generated constructor stub
 }

 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }

 @AfterClass
 public static void tearDownAfterClass() throws Exception {
 }

 @Before
 public void setUp() throws Exception {
 }

 @After
 public void tearDown() throws Exception {
 }
 
 @Test
 public void testSum(){
  
  MainActivity mainactivity = new MainActivity();
  
  int result = mainactivity.sum(3, 4);
  assertEquals(5,result);
 }

}

 

 

配置文件AndroidManifest.xml源码如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hkrt.quickpass.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
 <!-- **** JUnit测试专用,正式发布需要移除 *** -->
    <uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="hkrt.quickpass" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

 

后来找到是构造函数的事,于是把泛型去掉, 然后用不带参数的构造函数,在构造函数里调用super(“com.xxx.xxx”, Activity.class); 就Ok了。

只要把测试类中构造函数修改成

 public MainActivityTest() {
  super("hkrt.quickpass.MainActivity", MainActivity.class);
 }

问题得以解决

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值