Junit:使用PowerMock的MethodNotFoundException

时间:2014-09-23 06:27:29

标签: java exception junit powermock dbunit

在运行基于DBUnit的junit测试用例时,我得到以下异常:

java.lang.RuntimeException: Invoking the beforeTestMethod method on PowerMock test listener org.powermock.api.extension.listener.AnnotationEnabler@2a5f1994 failed.
    at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:92)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:292)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.powermock.reflect.exceptions.MethodNotFoundException: No method found with name 'assertNoAnnotations' with parameter types: [ java.lang.reflect.Field, [Ljava.lang.Class; ] in class org.powermock.api.mockito.internal.configuration.PowerMockitoInjectingAnnotationEngine.
    at org.powermock.reflect.internal.WhiteboxImpl.throwExceptionIfMethodWasNotFound(WhiteboxImpl.java:1247)
    at org.powermock.reflect.internal.WhiteboxImpl.findMethodOrThrowException(WhiteboxImpl.java:985)
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:882)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:713)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
    at org.powermock.api.mockito.internal.configuration.PowerMockitoInjectingAnnotationEngine.process(PowerMockitoInjectingAnnotationEngine.java:45)
    at org.powermock.api.extension.listener.AnnotationEnabler.injectSpiesAndInjectToSetters(AnnotationEnabler.java:55)
    at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:50)
    at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:90)
    ... 18 more

我的班级看起来像:

@RunWith(PowerMockRunner.class)
public class SampleDbTest extends {

    /** service mock */
    @InjectMocks
    private TestService testService;

    /** entity manager. */
    EntityManager                    entityManager;

    /**
     * Sets the up.
     * 
     * @throws Exception the exception
     */
    @Before
    public final void setUp() throws Exception {
        // get entity manager
        injectField(testService, "entityManager", entityManager);
    }
    ....
    ....

这里TestService是无状态会话bean接口。对于这个奇怪的异常,我无法在任何地方找到任何解释。如果您有任何想法,请告诉我。

感谢。

1 个答案:

答案 0 :(得分:3)

我认为您使用的是PowerMock和Mockito的不兼容版本。

如果您查看堆栈跟踪,您会看到PowerMockitoInjectingAnnotationEngine抛出了该异常,并且此类无法找到assertNoAnnotations方法。

Powermock的最新版本是1.5.6。因此,现在查看此类的src以获取较旧版本的PowerMock(例如1.4.12),您将看到已处理assertNoAnnotations方法的调用。从1.5版本开始,此课程不会使用此类调用。

Mockito的最新版本是1.9.5。现在,如果您查看InjectingAnnotationEngine的{​​{1}}(由PowerMockitoInjectingAnnotationEngine扩展)对于Mockito的1.9.0版本的src,您会看到,assertNoAnnotations方法仍然存在。并且,在1.9.5版本中 - 此方法已经消失。

我认为,您将通过更新您使用的框架版本来消除此错误。