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

初学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 {

 public MainAcvitityTest(Class 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源码如下:


    package="hkrt.quickpass.test"
    android:versionCode="1"
    android:versionName="1.0" >

   
 
   
            android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="hkrt.quickpass" />

            android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
       
   

 

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

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

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

问题得以解决

你可能感兴趣的:(单元测试,Andriod单元测试)