gpt4 book ai didi

testing - org.mockito.exceptions.misusing.UnfinishedStubbingException 检测到未完成的 stub

转载 作者:行者123 更新时间:2023-11-28 20:06:05 29 4
gpt4 key购买 nike

我写了下面的代码:

@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

编辑

参见 Powermock FAQ :

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

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 无效。请参阅下面的类(class)。第一种方法失败。第二个 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);
}
}

关于testing - org.mockito.exceptions.misusing.UnfinishedStubbingException 检测到未完成的 stub ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28437313/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com