org.mockito.exceptions.misusing.UnfinishedStubbingException检测到未完成的存根

时间:2015-02-10 16:53:40

标签: testing junit mocking mockito powermock

我写了以下代码:

@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.....

我错了什么?

2 个答案:

答案 0 :(得分:3)

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

修改

请参阅Powermock FAQ

  

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

     

这是因为它们是由Java的bootstrap类加载器加载而不能的   由PowerMock的类加载器操纵的字节码。以来   PowerMock 1.2.5有一个解决方法,请查看this   一个简单的例子,看看它是如何完成的。

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

第三种方法是我通常的解决方法,以避免静态或系统模拟。我只是创建一个包受保护的方法,我在我的测试中窥探它。我推荐一样的。它比涉及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);
    }
}

答案 1 :(得分:0)

似乎该方法可能已被嘲笑。

评论" Integer.parseInt"方法拦截线对我来说很好:

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

    @Test
    public void testStaticIntegerMocking2() {
        PowerMockito.mockStatic(Integer.class);
        when(Integer.decode(anyString())).thenReturn(new Integer(10023));
        System.out.println("HelloX..."+Integer.decode("#ffff"));
    }
}

似乎" 0"是这种情况下的默认模拟结果。 请注意:如果你返回一个"新的整数(...)"测试将执行没有错误,但仍然" 0"即使我们指定了不同的值,也会返回,例如new Integer(" 1002")。

但等等,还有更多!类似的测试工作正常,完全如预期,使用静态方法" Integer.decode(String)"!见上面的第二种方法。

相关问题