0

我是新的儀器測試。我正在嘗試使用AndroidJUnitRunner進行基本測試。這是我的gradle:Android概率與AndroidJUnitRunner給'java.lang.ClassNotFoundException'

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 24 
    buildToolsVersion "25.0.0" 
    defaultConfig { 
     applicationId "com.kaushik.mycart.ui" 
     minSdkVersion 15 
     targetSdkVersion 24 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
     jackOptions.enabled true 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    compileOptions { 
     sourceCompatibility JavaVersion.VERSION_1_8 
     targetCompatibility JavaVersion.VERSION_1_8 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 

    .......................... 
    .......................... 

    testCompile 'junit:junit:4.12' 
    testCompile 'org.mockito:mockito-core:1.10.19' 
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3' 

} 

然後我添加了一個名爲'ProductListActivityTest'的類來測試AndroidJunitRunner。它是如下:

package com.kaushik.myredmart.ui; 
// all includes 
@RunWith(AndroidJUnit4.class) 
public class ProductListActivityTest { 

    @Rule 
    public ActivityTestRule<ProductListActivity> rule = new ActivityTestRule<>(ProductListActivity.class); 

    @Test 
    public void ensureListViewIsPresent() throws Exception { 
     ProductListActivity activity = rule.getActivity(); 
     View viewByIdw = activity.findViewById(R.id.productListView); 
     assertThat(viewByIdw,notNullValue()); 
     assertThat(viewByIdw, instanceOf(RecyclerView.class)); 
     RecyclerView productListView = (RecyclerView) viewByIdw; 
     RecyclerView.Adapter adapter = productRecyclerView.getAdapter(); 
     assertThat(adapter, instanceOf(ProductAdapter.class)); 

    } 
} 

我在'androidTest'中沒有添加其他文件。我也想提及在我的源代碼中有Application類文件。現在,每當我嘗試運行測試時間,它給以下錯誤:

Started running tests 
Test running failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException' 
Empty test suite. 

誰能幫助我確定在我的測試代碼中的問題?

回答

0

我解決了這個問題。這是由於軟件包名稱的差異。我將所有包名稱統一併再次運行。有用。謝謝大家的幫助。

1

你如何運行你的測試?

如果您是從Android的工作室做:

  • 打開運行菜單 - >編輯配置
  • 添加一個新的Android測試配置
  • 選擇一個模塊
  • 添加特定的儀器亞軍:

    android.support.test.runner.AndroidJUnitRunner

從命令行通過搖籃:

./gradlew connectedAndroidTest