1

I am new in Instrumentation testing. I am trying basic testing with AndroidJUnitRunner. Here is my gradle:

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'

}

Then I added a class named 'ProductListActivityTest' to test with AndroidJunitRunner. It is below:

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

    }
}

I have added no other file in 'androidTest'. I like to also mention that there is Application class file in my source code. Now every time I try to run the test, it is giving following error:

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

Can anyone help me identifying the problem in my test code?

2 Answers 2

3

How are you running your tests?

If you are doing it from Android Studio:

  • Open Run menu -> Edit Configurations
  • Add a new Android Tests configuration
  • Choose a module
  • Add a specific instrumentation runner:

    android.support.test.runner.AndroidJUnitRunner

From command-line via Gradle:

./gradlew connectedAndroidTest
Sign up to request clarification or add additional context in comments.

Comments

0

I solved the problem. It is due to difference of Package names. I set all package name uniform and run again. It works. Thank you all for helping.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.