本文介绍了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:

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.

推荐答案

开发者的答案:

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

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

09-25 01:55