org.mockito.exceptions.misusing.UnfinishedStubbingException检测到未完成的存根 [英] org.mockito.exceptions.misusing.UnfinishedStubbingException Unfinished stubbing detected

查看:1308
本文介绍了org.mockito.exceptions.misusing.UnfinishedStubbingException检测到未完成的存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下代码:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Integer.class)
public class TestClass{


    @Test
        public void test(){
            PowerMockito.mockStatic(Integer.class);
            when(Integer.parseInt(anyString())).thenReturn(0);
            System.out.println(Integer.parseInt("12"));
        }
}

我收到以下错误消息:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at com.ctc.dime.services.autopublisher.stores.StoresPublishingServiceTest.test(StoresPublishingServiceTest.java:120)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, you naughty developer!

    at org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:291)
    at org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:193)
    at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)
    at org.powermock.core.MockGateway.methodCall(MockGateway.java:168)
b.....

我怎么了?

推荐答案

您应该准备使用系统类的类,而不是系统类本身.请参见 https://code.google.com/p/powermock/wiki/MockSystem

You should prepare the class that uses the system class, and not the system class itself. See https://code.google.com/p/powermock/wiki/MockSystem

请参见 Powermock常见问题解答:

我无法从java.lang,java.net,java.io或其他中模拟类 系统类,为什么?

I cannot mock classes in from java.lang, java.net, java.io or other system classes, why?

这是因为它们是由Java的引导类加载器加载的,因此无法 由PowerMock的类加载器操作的字节码.自从 PowerMock 1.2.5有一个解决方法,请查看

This is because they're loaded by Java's bootstrap classloader and cannot be byte-code manipulated by PowerMock's classloader. Since PowerMock 1.2.5 there's a work-around, please have a look at this simple example to see how it's done.

我做了一个小测试,无论出于什么原因,它似乎都适用于java.lang.String,但不适用于java.lang.Integer.请参阅下面的类.第一种方法失败.第二个使用String.format可以工作.对我来说,这似乎是一个Powermock错误.

I made a small test, and it seems to be working for java.lang.String but not for java.lang.Integer for whatever reason. See the classes below. The first method fails. The second with String.format works. This seems to be a Powermock bug for me.

第三个方法是我通常的解决方法,以避免静态或系统模拟.我只是创建一个受包保护的方法,然后在测试中对其进行监视.我推荐一样.比参与Powermock更好.

The third method is my usual workaround to avoid static or system mocking. I just create a package protected method, and I spy it in my test. I recommend the same. It is better than involving Powermock.

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest( { Dummy.class } )
public class TestClass{
    @Test
        public void testStaticIntegerMocking(){
            PowerMockito.mockStatic(Integer.class);
            when(Integer.parseInt(anyString())).thenReturn(0);
            System.out.println(Dummy.parseInt("12"));
        }

    @Test
    public void assertThatMockingStringWorks() throws Exception {
            PowerMockito.mockStatic(String.class);
            final String string = "string";
            final String args = "args";
            final String returnValue = "returnValue";

            when(String.format(string, args)).thenReturn(returnValue);

            final Dummy systemClassUser = new Dummy();
            assertEquals(systemClassUser.format(string, args), returnValue);
    }

    @Test
    public void testSpying(){
        Dummy dummy = new Dummy();
        dummy = spy(dummy);
        doReturn( 0 ).when(dummy).parseIntToBeSpyed(anyString());
        System.out.println(dummy.parseIntToBeSpyed("12"));
    }
}

虚拟类:

import java.io.IOException;

public class Dummy {
    public static Integer parseInt( String string ) {
        return Integer.parseInt(string);
    }

    public String format(String one, String args) throws IOException {
        return String.format(one, args);
    }

    public Integer parseIntToBeSpyed( String string ) {
        return Integer.parseInt(string);
    }
}

这篇关于org.mockito.exceptions.misusing.UnfinishedStubbingException检测到未完成的存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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