如何在Android单元测试中创建一个位置(Location)?

3
我希望能够使用JUnit 4在Android单元测试中创建位置(Location)。

使用 Location loc = new Location(...) 时,loc是null。

我该如何创建一个位置?

据我所知,我必须在build.gradle的依赖项中包括Location,使用 testCompile '...' 但我找不到要包含什么。

你能帮忙吗? 提前感谢。


你尝试过RoboElectric吗?这里有一个参考链接 - Daniel
你是在开发机上使用纯JUnit(即没有设备测试)进行单元测试吗?那行不通,编译时的Android库大多是存根。 - dhke
1个回答

0
在你的模块 build.gradle 中:
apply plugin: 'com.android.application'

android {
    ...
    testOptions {
        unitTests.returnDefaultValues = true
    }
}

dependencies {

    testImplementation 'junit:junit:4.13.2'
    testImplementation "org.robolectric:robolectric:4.2.1"
    ...
}

你的测试类必须包含以下内容:

@RunWith(RobolectricTestRunner.class)
public class LocationTest {
    ...
}

就是这样。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接