未模拟 Android 单元测试 [英] Android unit test not mocked

查看:52
本文介绍了未模拟 Android 单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了本指南https://sites.google.com/a/android.com/工具/技术文档/单元测试支持但我被这个错误困住了:

I followed this guide https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support but i am stuck with this error:

junit.framework.AssertionFailedError: Exception in constructor: testSaveJson (java.lang.RuntimeException: Method put in org.json.JSONObject not mocked. See https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support for details.

我按照指南说的那样通过 Gradle build 进行了修改,但没有任何区别

I modified by Gradle build like the guide says but it doesn't make a difference

 testOptions { 
    unitTests.returnDefaultValues = true
  }

推荐答案

JSON 与 Android SDK 捆绑在一起,因此您只会遇到存根问题.您可以拉入一个 JSON jar,它将提供要使用的真实对象.

JSON is bundled up with the Android SDK, so you'll just be hitting a stub. You can pull in a JSON jar, which will provide real objects to use.

为此,您需要将其添加到 build.gradle:

To do this, you'll need to add this to your build.gradle:

testImplementation 'org.json:json:20140107'

或者,您可以下载并包含 jar.

Alternatively, you can download and include the jar.

testCompile files('libs/json.jar')

请注意,最新版本的 JSON 是为 Java 8 构建的,因此您需要获取 20140107您可能还需要清理和重建项目.

Note that the latest version of JSON is built for Java 8, so you'll need to grab 20140107 You may also need to clean and rebuild the project.

这篇关于未模拟 Android 单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆