method d in android.util.log not mocked,Android Studio 单元测试报错 Method d in android.util.Log not mock...

在使用Android Studio进行Junit4单元测试时,遇到了一个异常:Method d in android.util.Log not mocked。问题出在自定义的Log类在测试环境中未被正确处理。测试时尝试调用了系统的Log.d方法,但该方法在测试环境下未被模拟。解决方案是使用Mockito等工具对Log类进行模拟,以便在测试中正确处理日志输出。
摘要由CSDN通过智能技术生成

在学习Android Studio进行单元测试的时候(Junit4), 进行如下测试时候

@Test

public void testParseLastPage() throws Exception {

String page = ToCollection.parseLastPage(Data.data);

assertEquals("Voucher.aspx?page=5", page);

}

报了错误如下

java.lang.RuntimeException: Method d in android.util.Log not mocked. See http://g.co/androidstudio/not-mocked for details.

at android.util.Log.d(Log.java)

at com.lantern.tools.Log.d(Log.java:21)

at com.lantern.data.deal.ToCollection.parseLastPage(ToCollection.java:36)

at com.lantern.data.deal.ToCollectionTest.testParseLastPage(ToCollectionTest.java:33)

Process finished with exit code 255

我个人的理解,是因为没有找到Log.d进行调用,还是因为在测试类ToCollectionTest在的包中也创建Log类,有点不明白为什么出现这个问题:

因为这个里面用到了要测试的方法是ToCollection类里面的parseLastPage(String html),解释:获得网页源代码,从html中解析出一个标签值;但是这个方法中当时写的时候,里面有自定义的Log类,方法的代码如下

public static String parseLastPage(String html) {

Log.d("Tag", "解析首页账单数据-解析a标签,获得对应数据");

Document doc = html2Document(html);

String page = "";

try {

page = doc.select("a").last().attr("href");

} catch (NullPointerException e) {

Log.d("String", "因为页面数量只有一个");

}

return page;

}

自定义的Log类是为了方便控制开发和发布时候的日志打印的,如下

public class Log {

private static final boolean DEBUG = true;

public static void i(String tag, String msg) {

if (DEBUG)

android.util.Log.i(tag, msg);

}

public static void e(String tag, String msg) {

if (DEBUG)

android.util.Log.e(tag, msg);

}

public static void d(String tag, String msg) {

if (DEBUG)

android.util.Log.d(tag, msg);

}

public static void v(String tag, String msg) {

if (DEBUG)

android.util.Log.v(tag, msg);

}

public static void w(String tag, String msg) {

if (DEBUG)

android.util.Log.w(tag, msg);

}

}

build.gradle如下

apply plugin: 'com.android.application'

android {

compileSdkVersion 22

buildToolsVersion "22.0.1"

defaultConfig {

applicationId "com.easy"

minSdkVersion 14

targetSdkVersion 22

versionCode 1

versionName "1.0"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

dependencies {

compile fileTree(include: ['*.jar'], dir: 'libs')

compile 'com.android.support:appcompat-v7:22.2.1'

compile files('src/main/libs/jsoup-1.8.3.jar')

}

repositories {

maven { url "https://jitpack.io" }

}

dependencies {

compile 'com.github.PhilJay:MPAndroidChart:v2.1.5'

// testCompile 意思为,test模式下依赖某一个库,该库不会在我们正式发布时打包到我们的程序中,作用和 debugCompile 类似。

testCompile 'junit:junit:4.12'

testCompile "org.mockito:mockito-core:1.9.5"

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值