Jmockit:无法模拟net.android.Uri类的toString方法 [英] Jmockit: Can't mock method toString of net.android.Uri class

查看:248
本文介绍了Jmockit:无法模拟net.android.Uri类的toString方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IDE:Android Studio 2.2.2 | Jmockit:2.1 | Java:1.8.0_102-b14

IDE: Android Studio 2.2.2 | Jmockit: 2.1 | Java: 1.8.0_102-b14

我试图模拟类net.android.Uri的toString()方法.此类上的所有方法都可以模拟OK,但可以使用toString()方法.

I tried to mock method toString() of class net.android.Uri. All method on this class could mock OK, but toString() method.

@Test
public void method(@Mocked final Uri uri) throws Exception {
    new NonStrictExpectations() {
            {
                uri.getHost();
                result = "sendViewLog";
                uri.getAuthority();
                result = "getAuthority";
                uri.getEncodedFragment();
                result = "getEncodedFragment";
                uri.getPort();
                result = 8080;
                uri.toString(); // <-------
                result = "helloWorld";
            }
        };      

    sut.method();
}

结果:但是toString()返回空值,上面的所有方法都被模拟了. 您能否给我一些解决方法来解决此问题.

Result: But toString() returns null value, all method above were mocked. Could you give me some solution method resolve this problem.

PS1: 我意识到,当我将鼠标悬停在Expectations块内的toString方法时.它显示以下警告消息:

PS1: I realized that when i hover to toString method inside Expectations block. It shows this warning message:

报告对特定方法的所有调用,这些调用的结果是 忽略了.检查设置中指定的两种方法和 用org.jetbrains.annotations.Contract注释的方法(pure = true) 被检查.对于许多方法,完全忽略结果 合法的,但是对于某些方法几乎可以肯定是一个错误. 可能忽略调用结果的方法的示例 是一个错误,包括java.io.inputStream.read(),它将返回 实际读取的字节数,java.lang.String上的任何方法或 java.math.BigInteger,因为所有这些方法都是无副作用的,并且 因此,如果被忽略则毫无意义.

Reports any calls to specific methods where the result of that call is ignored. Both methods specified in the inspection's settings and methods annotated with org.jetbrains.annotations.Contract(pure=true) are checked. For many methods, ignoring the result is perfectly legitimate, but for some methods it is almost certainly an error. Examples of methods where ignoring the result of a call is likely to be an error include java.io.inputStream.read(), which returns the number of bytes actually read, any method on java.lang.String or java.math.BigInteger, as all of those methods are side-effect free and thus pointless if ignored.

PS2: 当我通过uri实例检查null时,它通过了. 但是,我要检查的来自客户端的代码是 uri.toString().contains("/video") 原因 uri.toString() == null,所以包含("/video")引发NullPointerException.

PS2: When I check null by uri instance, it passes. But, code from client which I want to check is uri.toString().contains("/video") Cause uri.toString() == null, so contains("/video") throws NullPointerException.

推荐答案

开发者的答案:

是的,只能模拟(使用主体而不是抽象的)toString()方法.

Yes, only implemented (with a body, not abstract) toString() methods can be mocked.

https://github.com/jmockit/jmockit1/issues/381

这篇关于Jmockit:无法模拟net.android.Uri类的toString方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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