【发布时间】:2014-04-10 07:10:58
【问题描述】:
我用 Java 开发了一个应用程序,我正在尝试使用 Powermockito 创建单元测试(我应该补充一点,我是单元测试的新手)。
我有一个名为 Resource 的类,它有一个名为 readResources 的静态方法:
public static void readResources(ResourcesElement resourcesElement);
ResourcesElement 也是我编写的。 在测试中,我想创建自己的Resource,所以我希望上面的方法什么都不做。 我尝试使用此代码:
PowerMockito.spy(Resource.class);
PowerMockito.doNothing().when(Resource.class, "readResources", Matchers.any(ResourcesElement.class));
单元测试抛出异常:
org.mockito.exceptions.misusing.UnfinishedStubbingException: 此处检测到未完成的存根: -> 在 org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)
Powermockito 还建议我应该在 when 之后使用 thenReturn 或 thenThrow,但似乎方法 'when' 在 doNothing 之后调用时返回 void(这是合乎逻辑的)。 如果我尝试:
PowerMockito.when(Resource.class, "readResources", Matchers.any(ResourcesElement.class)).....
doNothing 不是在 when 之后的选项。
我设法使没有参数的方法什么都不做,使用该方法的 2 参数版本。例如:
PowerMockito.doNothing().when(Moduler.class, "startProcessing");
这行得通(startProcessing 不接受任何参数)。
但是我怎样才能使带有参数的方法对 Powermockito 不做任何事情呢?
【问题讨论】:
标签: java unit-testing powermock