【问题标题】:How can I create a Location in an android Unit-Test?如何在 android 单元测试中创建位置?
【发布时间】:2021-03-06 18:58:51
【问题描述】:

我想使用 JUnit 4 在 android 单元测试中创建一个位置。

与 位置 loc = 新位置(...) loc 为空。

如何创建位置?

据我了解,我必须在 build.gradle 依赖项中包含 Location 使用 测试编译'...' 但我找不到要包含的内容。

你能帮忙吗? 提前致谢。

【问题讨论】:

  • 你试过RoboElectric吗?这是reference
  • 您正在在开发机器上使用纯 JUnit 进行单元测试(即没有进行设备测试)?那行不通,编译时的 android 库大多是存根。

标签: android junit


【解决方案1】:

在你的模块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 {
    ...
}

应该就是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多