1

I am trying to implement Robolectric framework in my Android app for Unit Testing purpose. I am first time working on the Robolectric, I followed below post and tutorial to understand the concept and implementation steps--

1] http://robolectric.org/getting-started/

2] https://android.jlelse.eu/robolectric-unit-testing-framework-for-android-b78ebac0b411

But when I am trying to run the first unit test, I am getting below error--

Process finished with exit code 1
Class not found: "com.xyz.my_package.activity.WelcomeActivityTest"

Below is my module level build.gradle file--

  apply plugin: 'com.android.application'

    android {
        compileSdkVersion 29
        buildToolsVersion '29.0.2'
        defaultConfig {
            applicationId "com.xyz.my_package"
            minSdkVersion 19
            targetSdkVersion 29
            versionCode 86
            versionName "86.0"
            multiDexEnabled true
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }

        dexOptions {
            preDexLibraries = false
        }

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }

        lintOptions {
            disable 'MissingTranslation'
        }

        sourceSets {
            main {
                assets.srcDirs = ['src/main/assets', 'src/main/assets/']
                res.srcDirs = ['src/main/res', 'src/main/res/drawable']
            }
        }


        testOptions {
            unitTests {
                includeAndroidResources = true
            }
        }
    }

    dependencies {

        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        implementation 'androidx.legacy:legacy-support-v4:1.0.0'
        implementation 'com.google.android.gms:play-services-maps:11.0.2'

        implementation 'com.google.android.gms:play-services-analytics:17.0.0'
        implementation 'com.google.android.gms:play-services-wallet:18.0.0'

        implementation 'com.facebook.fresco:fresco:1.9.0'

        testImplementation('androidx.test.espresso:espresso-core:3.1.0', {
            exclude group: 'com.android.support', module: 'support-annotations'
            exclude module: 'support-v4' exclude module: 'support-v13' exclude module: 'recyclerview-v7'
        })
        implementation 'androidx.appcompat:appcompat:1.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test.ext:junit:1.1.0'
        testImplementation 'org.robolectric:robolectric:4.3'
        testImplementation 'androidx.test:core:1.0.0'

        implementation 'com.amplitude:android-sdk:2.16.0'
        implementation 'com.google.zxing:core:3.2.1'
        implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
        implementation 'com.google.android.material:material:1.0.0'
        implementation project(":liblknscratcherview")
        implementation 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
        implementation 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
        implementation 'commons-io:commons-io:2.4'
        implementation 'com.google.code.gson:gson:+'
        implementation 'org.apache.httpcomponents:httpcore:4.4.6'
        implementation 'com.google.android.gms:play-services-location:17.0.0'
        implementation 'com.squareup.picasso:picasso:2.5.2'
        implementation 'com.squareup.retrofit:retrofit:1.9.0'
        implementation 'com.daimajia.swipelayout:library:1.2.0@aar'
        implementation 'com.kontaktio:sdk:3.3.3'
        implementation 'io.gresse.hugo.vumeterlibrary:vumeterlibrary:1.0.15'
        implementation 'androidx.recyclerview:recyclerview:1.0.0'
        implementation 'com.mindorks:placeholderview:0.2.7'
        implementation 'androidx.cardview:cardview:1.0.0'
        implementation 'com.facebook.shimmer:shimmer:0.1.0@aar'
        implementation 'com.sothree.slidinguppanel:library:3.3.1'

        implementation 'androidx.multidex:multidex:2.0.0'
        implementation 'com.facebook.android:facebook-login:[4,5)'
        implementation 'com.facebook.android:facebook-share:[4,5)'

        implementation 'com.facebook.android:facebook-android-sdk:[4,5)'
        implementation 'bz.tsung.android:objectify:2.0'
        implementation 'com.xiaofeng.android:flowlayoutmanager:1.2.3.2'
        implementation 'de.hdodenhof:circleimageview:3.0.0'

        implementation(name: 'core-sdk_4_4_1', ext: 'aar')
        implementation(name: 'audio-sdk_4_4_1', ext: 'aar')
        implementation(name: 'beacon-sdk_4_4_1', ext: 'aar')
        implementation(name: 'geofence-sdk_4_4_1', ext: 'aar')

        implementation project(":library")
    }

Below is my Unit Test class "WelcomeActivityTest.java" for which I am getting above exception--

@RunWith(RobolectricTestRunner.class)
public class WelcomeActivityTest
{
    private Activity activity;

    @Before
    public void setup()
    {
        activity = Robolectric.buildActivity(WelcomeActivity.class).create().get();
    }

    @Test
    public void emailSignUpBtnClick()
    {
        Button signUpEmail = (Button) activity.findViewById(R.id.signUpEmail);

        signUpEmail.performClick();
        ShadowActivity shadowActivity = shadowOf(activity);
        Intent startedIntent = shadowActivity.getNextStartedActivity();
        ShadowIntent shadowIntent = shadowOf(startedIntent);
        assertThat(shadowIntent.getIntentClass().getName(), 
        equalTo(SignUpEmailActivity.class.getName()));
    }
}

-- I am correctly writing unit test classes in "app/src/test/" directory.

-- I am correctly maintaining same package hierarchy for my actual classes and test classes e.g.

"com.xyz.my_package.activity.WelcomeActivity" is my actual class written in "app/src/main/java/" directory

And 

"com.xyz.my_package.activity.WelcomeActivityTest" is my test class written in "app/src/test/java/" directory

Below is the "Rub/Debug Confuguration" screen-shot--

enter image description here

Below is what I tried so-far--

1] Created separate demo app and tried running the same test case there and the strange thing is, it is working fine in Demo app but getting "Class not found" exception for the Test class in my actual app.

2] Compared the gradle dependencies and "Rub/Debug Confuguration" setting with the Demo app's gradle and "Rub/Debug Confuguration" setting and they are same.

3] Tries all the solution available on the this post but unfortunately none of them worked for me.

Noticeable things:

1] My project is created 2 years back in Android Studio 3.0.0 version and currently I am using 3.6.3 version.

2] My Demo app which is working fine is created in Android Studio 3.6.3 version.

I hope given details are enough to trace the problem, still if you want some more details, please let me know. Thank you!

Dnyanesh M
  • 1,349
  • 4
  • 18
  • 46

0 Answers0