Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在测试中使用位置对象?_Android_Unit Testing_Kotlin_Location - Fatal编程技术网

Android 如何在测试中使用位置对象?

Android 如何在测试中使用位置对象?,android,unit-testing,kotlin,location,Android,Unit Testing,Kotlin,Location,我试图编写测试来测试一些代码操作对象。 使用@Before JUnit注释,我想通过以下方式初始化一个位置实例: @以前 趣味初始服务{ 位置=LocationLocationManager.GPS\u提供程序 System.out.printlinit$位置 } 执行测试时,输出不是很令人满意,打印:init null 知道这段代码在经典上下文中工作,有没有一种特殊的方法可以在测试上下文中初始化对象实例?要测试Android特定的代码,需要隐藏SDK类。你可以用。只需向build.gradle

我试图编写测试来测试一些代码操作对象。 使用@Before JUnit注释,我想通过以下方式初始化一个位置实例:

@以前 趣味初始服务{ 位置=LocationLocationManager.GPS\u提供程序 System.out.printlinit$位置 } 执行测试时,输出不是很令人满意,打印:init null


知道这段代码在经典上下文中工作,有没有一种特殊的方法可以在测试上下文中初始化对象实例?

要测试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)
    }
}