0

I have successfully migrated to Androidx, everything seems to work. Now I'm stuck in migrating (or basically creating tests) for my project.

I added those dependencies to my project:

    implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation 'androidx.appcompat:appcompat:1.1.0'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  testImplementation 'junit:junit:4.12'
  androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
  androidTestImplementation 'androidx.test:rules:1.2.0'
  androidTestImplementation 'androidx.test:runner:1.2.0'

  implementation 'androidx.multidex:multidex:2.0.1'
  implementation 'androidx.cardview:cardview:1.0.0'
  implementation 'com.google.android.material:material:1.2.0-alpha03'

  implementation 'com.google.android.gms:play-services-maps:17.0.0'
  implementation 'com.google.android.gms:play-services-location:17.0.0'
  implementation 'com.google.android.gms:play-services-identity:17.0.0'
  implementation 'com.google.android.gms:play-services-gcm:17.0.0'
  implementation 'joda-time:joda-time:2.9.9'
  implementation 'com.google.code.gson:gson:2.8.5'
  implementation 'ch.acra:acra:4.6.1'
  implementation 'com.squareup.okhttp3:okhttp:4.2.1'
  implementation 'com.diegocarloslima:fgelv:0.1.2'
  implementation 'com.luckycatlabs:SunriseSunsetCalculator:1.1'
  implementation project(':cp963_cpik_lib') 

I created two paths for two tests (one is for test which works fine and Android studio recognize the @Test annotation of junit class, the other is for androidTest which Android studio does not recognize any of the classes). Both test and androidTest are green and have the same package name as my src folder.

This is the test file (works just fine):

    import org.junit.Test;

import static org.junit.Assert.*;

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
public class ExampleUnitTest {
    @Test
    public void addition_isCorrect() {
        assertEquals(4, 2 + 2);
    }
}

This is androidTest that I'm not sure why Android Studio is not able to import any of the classes

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        assertEquals("xxxxxx", appContext.getPackageName());
    }
}

The classes that I mean are @RunWith, AndroidJUnit4, @Test, InstrumentationRegistry, assertEquals.

I basically used the generated tests that came with a new project. Do I need somehow to configure Android Studio to use junit in androidTest? or is it something to do with the copilot library that I'm using?

Thank you

Edit 1

I added the previous test that was in my project:

public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
    super(Application.class);
}

} It's an old project which I'm trying to use more up to date dependencies and unit testing.

Edit 2 This is my build.gradle.

apply plugin: 'com.android.application'

android {

  compileSdkVersion 28
  buildToolsVersion '28.0.3'
  defaultConfig {
    applicationId "xxx.xxx.xxx"
    minSdkVersion 19 // 17 = 4.2 Jelly Bean, 18 = 4.3 jelly bean, 19 = 4.4 kitkat
    //noinspection ExpiredTargetSdkVersion
    targetSdkVersion 19 // 21 = 5.1 Lollipop
    versionCode 65
    versionName "1.65"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
  }
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      signingConfig signingConfigs.config
    }
    debug {
      signingConfig signingConfigs.config
    }
  }
}

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation 'androidx.appcompat:appcompat:1.1.0'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  testImplementation 'junit:junit:4.12'
  androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
  androidTestImplementation 'androidx.test:rules:1.2.0'
  androidTestImplementation 'androidx.test:runner:1.2.0'

  implementation 'androidx.multidex:multidex:2.0.1'
  implementation 'androidx.cardview:cardview:1.0.0'
  implementation 'com.google.android.material:material:1.2.0-alpha03'

  implementation 'com.google.android.gms:play-services-maps:17.0.0'
  implementation 'com.google.android.gms:play-services-location:17.0.0'
  implementation 'com.google.android.gms:play-services-identity:17.0.0'
  implementation 'com.google.android.gms:play-services-gcm:17.0.0'
  implementation 'joda-time:joda-time:2.9.9'
  implementation 'com.google.code.gson:gson:2.8.5'
  implementation 'ch.acra:acra:4.6.1'
  implementation 'com.squareup.okhttp3:okhttp:4.2.1'
  implementation 'com.diegocarloslima:fgelv:0.1.2'
  implementation 'com.luckycatlabs:SunriseSunsetCalculator:1.1'

  implementation project(':cp963_cpik_lib')
  androidTestImplementation 'junit:junit:4.12' // I tried with this and without but I got the same result.
}
DemoDemo
  • 372
  • 1
  • 12
  • Make sure that you have `import androidx.test.ext.junit.runners.AndroidJUnit4` instead of `import androidx.test.runner.AndroidJUnit4`. Other than that your `ExampleInstrumentedTest` checks out correctly and the problem would be somewhere else. Could you post the complete `build.gradle` file? – ahasbini Dec 23 '19 at 16:08
  • I updated my post, I tried to import androidx.test.ext.junit.runners.AndroidJUnit4 but it says cannot resolve symbol test – DemoDemo Dec 23 '19 at 16:49
  • This is weird, according to this https://stackoverflow.com/a/52776938/2949966 it should be found even with your current `build.gradle` dependencies. Try performing a clean and rebuild. Make sure to choose the `debug` build variant in the Build Variants window of Android Studio. – ahasbini Dec 23 '19 at 16:54
  • I restarted android studio with invalidated cache and now it seems to work!, really strange behavior. Thanks for your time to help me out! – DemoDemo Dec 23 '19 at 17:28

0 Answers0