Powermock - 无法模拟ScheduledExecutorService

时间:2017-04-18 08:00:45

标签: powermock powermockito

我无法模拟ScheduledExcecutorService。不确定我在哪里弄错了。关于异常的一个奇怪的部分是“无法在对象层次结构中找到方法调度”,为什么它试图在Object类中找到方法而不是ScheduledExecutorService?

任何帮助将不胜感激。提前致谢。

@RunWith(PowerMockRunner.class)
@PrepareForTest({ ScheduledExecutorService.class, GenericCache.class, ConfigurationDAO.class, ScheduledFuture.class })
@PowerMockIgnore("javax.management.*")
public class CacheManagerTestCase {

// Fields 

@Mock
private ScheduledExecutorService mockScheduledExecutorService;

@Mock
private ScheduledFuture scheduledFutureMock;


// Within Test method

when(mockScheduledExecutorService
                        .schedule(any(Runnable.class), anyLong(), eq(TimeUnit.SECONDS)))
                        .thenReturn(scheduledFutureMock);

运行上面的代码时,抛出以下异常

org.powermock.reflect.exceptions.MethodNotFoundException: No methods matching the name(s) schedule were found in the class hierarchy of class java.lang.Object.
at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1720)
at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1745)
at org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:983)
at org.powermock.core.MockGateway$MockInvocation.findMethodToInvoke(MockGateway.java:317)
at org.powermock.core.MockGateway$MockInvocation.init(MockGateway.java:356)
at org.powermock.core.MockGateway$MockInvocation.<init>(MockGateway.java:307)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:142)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:125)
at CacheManagerTestCase.testPeriodicRefreshPropertyFalse(CacheManagerTestCase.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)



org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
0 matchers expected, 3 recorded:
-> at CacheManagerTestCase.testPeriodicRefreshPropertyFalse(CacheManagerTestCase.java:82)
-> at CacheManagerTestCase.testPeriodicRefreshPropertyFalse(CacheManagerTestCase.java:82)
-> at CacheManagerTestCase.testPeriodicRefreshPropertyFalse(CacheManagerTestCase.java:82)

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

0 个答案:

没有答案