无法使用powermockito模拟静态方法?

时间:2013-03-22 06:36:07

标签: java junit mockito powermock

获得例外:

although stubbed methods may return mocks, you cannot inline mock creation (mock()) call inside a thenReturn method (see issue 53)

我正在关注https://code.google.com/p/powermock/wiki/MockitoUsage

PowerMockito.mockStatic(MyUtil.class);

Mockito.when(MyUtil.compareVersion("1","2")).thenReturn(200);

另外我想模拟这个类的部分,这个类中有其他方法,我不想模拟,它们应该像往常一样运行。

详细例外:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:31)

1 个答案:

答案 0 :(得分:0)

我无法从您的代码段中看出,但您的测试类中是否有以下注释?

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyUtil.class)
public class MyUtilTest {

此外,您应该查看https://code.google.com/p/powermock/wiki/MockitoUsage13以获取更新版本的PowerMock。

相关问题