Skip to content

Commit 15b0fe2

Browse files
XinyueZnic0lette
authored andcommitted
Fix androidTest with new arch-testing libs (android#412)
1 parent bf8ff7c commit 15b0fe2

File tree

7 files changed

+12
-10
lines changed

7 files changed

+12
-10
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ dependencies {
7373
androidTestImplementation "androidx.test.espresso:espresso-contrib:$rootProject.espressoVersion"
7474
androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.espressoVersion"
7575
androidTestImplementation "androidx.test.espresso:espresso-intents:$rootProject.espressoVersion"
76+
androidTestImplementation "androidx.test.ext:junit:$rootProject.testExtJunit"
7677
androidTestImplementation "androidx.test.uiautomator:uiautomator:$rootProject.uiAutomatorVersion"
7778
androidTestImplementation "androidx.work:work-testing:$rootProject.workVersion"
7879
androidTestImplementation "com.google.truth:truth:$rootProject.truth"

app/src/androidTest/java/com/google/samples/apps/sunflower/GardenActivityTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.google.samples.apps.sunflower
1818

1919
import android.view.Gravity
20-
import androidx.test.InstrumentationRegistry
2120
import androidx.test.espresso.Espresso.onView
2221
import androidx.test.espresso.action.ViewActions
2322
import androidx.test.espresso.action.ViewActions.click
@@ -29,6 +28,7 @@ import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
2928
import androidx.test.espresso.matcher.ViewMatchers.isRoot
3029
import androidx.test.espresso.matcher.ViewMatchers.withContentDescription
3130
import androidx.test.espresso.matcher.ViewMatchers.withId
31+
import androidx.test.platform.app.InstrumentationRegistry
3232
import androidx.test.rule.ActivityTestRule
3333
import androidx.test.uiautomator.UiDevice
3434
import com.google.samples.apps.sunflower.utilities.getToolbarNavigationContentDescription

app/src/androidTest/java/com/google/samples/apps/sunflower/PlantDetailFragmentTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import android.accessibilityservice.AccessibilityService
2020
import android.content.Intent
2121
import android.os.Bundle
2222
import androidx.navigation.findNavController
23-
import androidx.test.InstrumentationRegistry
23+
import androidx.test.ext.junit.runners.AndroidJUnit4
24+
import androidx.test.platform.app.InstrumentationRegistry
2425
import androidx.test.espresso.Espresso.onView
2526
import androidx.test.espresso.action.ViewActions.click
2627
import androidx.test.espresso.intent.Intents
@@ -30,7 +31,6 @@ import androidx.test.espresso.intent.matcher.IntentMatchers.hasExtra
3031
import androidx.test.espresso.intent.matcher.IntentMatchers.hasType
3132
import androidx.test.espresso.matcher.ViewMatchers.withId
3233
import androidx.test.rule.ActivityTestRule
33-
import androidx.test.runner.AndroidJUnit4
3434
import com.google.samples.apps.sunflower.utilities.chooser
3535
import com.google.samples.apps.sunflower.utilities.testPlant
3636
import org.hamcrest.CoreMatchers.allOf

app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package com.google.samples.apps.sunflower.data
1818

1919
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
2020
import androidx.room.Room
21-
import androidx.test.InstrumentationRegistry
2221
import androidx.test.espresso.matcher.ViewMatchers.assertThat
22+
import androidx.test.platform.app.InstrumentationRegistry
2323
import com.google.samples.apps.sunflower.utilities.getValue
2424
import com.google.samples.apps.sunflower.utilities.testCalendar
2525
import com.google.samples.apps.sunflower.utilities.testGardenPlanting
@@ -41,7 +41,7 @@ class GardenPlantingDaoTest {
4141
var instantTaskExecutorRule = InstantTaskExecutorRule()
4242

4343
@Before fun createDb() {
44-
val context = InstrumentationRegistry.getTargetContext()
44+
val context = InstrumentationRegistry.getInstrumentation().targetContext
4545
database = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build()
4646
gardenPlantingDao = database.gardenPlantingDao()
4747

app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package com.google.samples.apps.sunflower.data
1818

1919
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
2020
import androidx.room.Room
21-
import androidx.test.InstrumentationRegistry
22-
import androidx.test.runner.AndroidJUnit4
21+
import androidx.test.ext.junit.runners.AndroidJUnit4
22+
import androidx.test.platform.app.InstrumentationRegistry
2323
import com.google.samples.apps.sunflower.utilities.getValue
2424
import org.hamcrest.Matchers.equalTo
2525
import org.junit.After
@@ -41,7 +41,7 @@ class PlantDaoTest {
4141
var instantTaskExecutorRule = InstantTaskExecutorRule()
4242

4343
@Before fun createDb() {
44-
val context = InstrumentationRegistry.getTargetContext()
44+
val context = InstrumentationRegistry.getInstrumentation().targetContext
4545
database = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build()
4646
plantDao = database.plantDao()
4747

app/src/androidTest/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModelTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package com.google.samples.apps.sunflower.viewmodels
1818

1919
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
2020
import androidx.room.Room
21-
import androidx.test.InstrumentationRegistry
21+
import androidx.test.platform.app.InstrumentationRegistry
2222
import com.google.samples.apps.sunflower.data.AppDatabase
2323
import com.google.samples.apps.sunflower.data.GardenPlantingRepository
2424
import com.google.samples.apps.sunflower.data.PlantRepository
@@ -40,7 +40,7 @@ class PlantDetailViewModelTest {
4040

4141
@Before
4242
fun setUp() {
43-
val context = InstrumentationRegistry.getTargetContext()
43+
val context = InstrumentationRegistry.getInstrumentation().targetContext
4444
appDatabase = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build()
4545

4646
val plantRepo = PlantRepository.getInstance(appDatabase.plantDao())

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ buildscript {
4343
runnerVersion = '1.0.1'
4444
supportLibraryVersion = '1.1.0-alpha05'
4545
truth = '0.42'
46+
testExtJunit = '1.1.0'
4647
uiAutomatorVersion = '2.2.0'
4748
workVersion = '2.1.0-alpha02'
4849
}

0 commit comments

Comments
 (0)