Unit testing 如何在使用Mockito进行测试之前强制初始化应用程序类中的上下文
这是我的申请课Unit testing 如何在使用Mockito进行测试之前强制初始化应用程序类中的上下文,unit-testing,kotlin,mockito,Unit Testing,Kotlin,Mockito,这是我的申请课 class MainApplication : MultiDexApplication() { init { INSTANCE = this } companion object { lateinit var INSTANCE: MainApplication private set val applicationContext: Context get() { return INSTANCE.app
class MainApplication : MultiDexApplication() {
init { INSTANCE = this }
companion object {
lateinit var INSTANCE: MainApplication
private set
val applicationContext: Context get() { return INSTANCE.applicationContext }
}
override fun onCreate() {
super.onCreate()
}
}
在我的测试中,我必须在我的CurrentCityViewModel中读取来自这里的实时位置:
class LocationLiveData(applicationContext: Context) : LiveData<LocationModel>() {
private var fusedLocationClient = LocationServices.getFusedLocationProviderClient(applicationContext)
....
}
class LocationLiveData(applicationContext:Context):LiveData(){
私有变量fusedLocationClient=LocationServices.getFusedLocationProviderClient(applicationContext)
....
}
这是我的测试类CurrentCityViewModelTest
@RunWith(KotlinTestRunner::class)
@OpenedPackages("com.hend.weatherforecastchallenge")
class CurrentCityViewModelTest {
@get:Rule
val mockitoRule: MockitoRule = MockitoJUnit.rule()
@get:Rule
val taskExecutorRule = InstantTaskExecutorRule()
@get:Rule
val rxSchedulerRule = RxSchedulerRule()
// Test rule for making the RxJava to run synchronously in unit test
companion object {
@ClassRule
@JvmField
val schedulers = RxImmediateSchedulerRule()
}
@Mock
lateinit var networkApiService: NetworkApiServices
@Mock
lateinit var observer: Observer<CityForecast>
@Mock
lateinit var currentCityViewModel: CurrentCityViewModel
@Before
fun setUp() {
currentCityViewModel = CurrentCityViewModel()
}
@Test
fun `init set forecast object to null`() {
val forecast =
currentCityViewModel.getCityForecast(LocationModel(37.5597, 55.4997))
.testObserver()
Truth.assert_()
.that(forecast.observedValues).isNull()
}
@Test
fun `getCityForecast set correct loading states`() {
val status = currentCityViewModel.uiState.testObserver()
currentCityViewModel.getCityForecast(LocationModel(37.5597, 55.4997))
Truth.assert_()
.that(status.observedValues)
.isEqualTo(listOf(Loading, HasData))
}
@Test
fun `getCityForecast set correct location`() {
val cityForecast =
currentCityViewModel.getCityForecast(LocationModel(37.5597, 55.4997)).testObserver()
Truth.assert_()
.that(cityForecast.observedValues)
.isEqualTo(LocationModel(37.5597, 55.4997))
}
@Test
fun `should show CityForecast data`() {
val locationModel = Mockito.mock(
LocationModel(37.5597, 55.4997)::class.java
)
val cityForecast = Mockito.mock(
CityForecast(
listOf(
Forecast(
Temperature(273.1, 265.32, 273.1, 273.1, 1011, 38),
listOf(Weather(803, "Clouds", "broken clouds", "04d")),
Wind(6.49, 313),
"12-3-2020"
)
), City(1, "Dubai", Coordinates(37.5597, 55.4997), "AE", 10800)
)::class.java
)
// make the getCityWeather api to return mock data
`when`(networkApiService.getFiveDaysWeather(37.5597, 55.4997))
.thenReturn(Single.just(cityForecast))
// observe on the MutableLiveData with an observer
currentCityViewModel._cityForecast.observeForever { observer }
currentCityViewModel.getCityForecast(locationModel)
}
}
@RunWith(KotlinTestRunner::class)
@开放包(“com.hend.weatherforecastchallenge”)
类CurrentCityViewModelTest{
@获取:规则
val mockitoRule:mockitoRule=MockitoJUnit.rule()
@获取:规则
val taskExecutorRule=InstantTaskExecutorRule()
@获取:规则
val rxSchedulerRule=rxSchedulerRule()
//使RxJava在单元测试中同步运行的测试规则
伴星{
@阶级规则
@JvmField
val schedulers=rximimediateschedulerrule()
}
@嘲弄
lateinit var networkApiService:NetworkApiServices
@嘲弄
lateinit var观察者:观察者
@嘲弄
lateinit变量currentCityViewModel:currentCityViewModel
@以前
趣味设置(){
currentCityViewModel=currentCityViewModel()
}
@试验
fun`init将预测对象设置为null`(){
val预测=
currentCityViewModel.getCityForecast(LocationModel(37.5597,55.4997))
.testObserver()
真理。断言
.that(forecast.observedValues).isNull()
}
@试验
fun`getCityForecast设置正确的加载状态`(){
val status=currentCityViewModel.uiState.testObserver()
currentCityViewModel.getCityForecast(LocationModel(37.5597,55.4997))
真理。断言
.即(状态.观测值)
.isEqualTo(列表(加载,HasData))
}
@试验
fun`getCityForecast设置正确位置`(){
瓦尔城市预测=
currentCityViewModel.getCityForecast(LocationModel(37.5597,55.4997)).testObserver()
真理。断言
.即(城市预测。观测值)
.isEqualTo(位置模型(37.5597,55.4997))
}
@试验
fun`应该显示城市预测数据'(){
val locationModel=Mockito.mock(
LocationModel(37.5597,55.4997)::class.java
)
val cityForecast=Mockito.mock(
城市预测(
列表(
预测(
温度(273.1,265.32,273.1,273.1,1011,38),
天气(803,“云”、“碎云”、“04d”),
风力(6.49313),
"12-3-2020"
)
),城市(1,“迪拜”,坐标(37.5597,55.4997),“AE”,10800)
)::class.java
)
//使getCityWeather api返回模拟数据
`当`(networkApiService.getFiveDaySweagher(37.5597,55.4997))
.thenReturn(单个.just(城市预测))
//使用观察者观察可变LiveData
currentCityViewModel._cityForecast.observeForever{observer}
currentCityViewModel.getCityForecast(locationModel)
}
}
我在测试类时遇到了一个奇怪的异常
kotlin.UninitializedPropertyAccessException: lateinit property INSTANCE has not been initialized
at com.hend.weatherforecastchallenge.MainApplication$Companion.getINSTANCE(MainApplication.kt:13)
at com.hend.weatherforecastchallenge.MainApplication$Companion.getApplicationContext(MainApplication.kt:16)
at com.hend.weatherforecastchallenge.ui.currentcity.CurrentCityViewModel.<init>(CurrentCityViewModel.kt:34)
at com.hend.weatherforecastchallenge.ui.currentcity.CurrentCityViewModelTest.setUp(CurrentCityViewModelTest.kt:61)
kotlin.UninitializedPropertyAccessException:lateinit属性实例尚未初始化
在com.hend.weatherforecastchallenge.MainApplication$Companion.getINSTANCE上(MainApplication.kt:13)
在com.hend.weatherforecastchallenge.MainApplication$Companion.getApplicationContext(MainApplication.kt:16)
在com.hend.weatherforecastchallenge.ui.currentcity.CurrentCityViewModel.(CurrentCityViewModel.kt:34)
在com.hend.weatherforecastchallenge.ui.currentcity.CurrentCityViewModelTest.setUp上(CurrentCityViewModelTest.kt:61)
在运行测试之前,我试图获取上下文,但没有用。有人帮我吗