Android 如何在测试中使用位置对象?
我试图编写测试来测试一些代码操作对象。 使用@Before JUnit注释,我想通过以下方式初始化一个位置实例: @以前 趣味初始服务{ 位置=LocationLocationManager.GPS\u提供程序 System.out.printlinit$位置 } 执行测试时,输出不是很令人满意,打印:init nullAndroid 如何在测试中使用位置对象?,android,unit-testing,kotlin,location,Android,Unit Testing,Kotlin,Location,我试图编写测试来测试一些代码操作对象。 使用@Before JUnit注释,我想通过以下方式初始化一个位置实例: @以前 趣味初始服务{ 位置=LocationLocationManager.GPS\u提供程序 System.out.printlinit$位置 } 执行测试时,输出不是很令人满意,打印:init null 知道这段代码在经典上下文中工作,有没有一种特殊的方法可以在测试上下文中初始化对象实例?要测试Android特定的代码,需要隐藏SDK类。你可以用。只需向build.gradle
知道这段代码在经典上下文中工作,有没有一种特殊的方法可以在测试上下文中初始化对象实例?要测试Android特定的代码,需要隐藏SDK类。你可以用。只需向build.gradle添加依赖项,并用@runWithRobolectrictTestRunner::class注释测试类
您可以共享完整的测试类文件吗?
import android.location.Location
import android.location.LocationManager
import org.junit.Before
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
class Test {
@Before
fun init_service() {
val location = Location(LocationManager.GPS_PROVIDER)
location.latitude = 22.234
location.longitude = 23.394
println(location)
}
}