在具有 Spring 的 JUnit 中,如何使用 Mockito (1.10.18) 在 @Service 上创建间谍?

In JUnit with Spring, how do I create a spy on a @Service using Mockito (1.10.18)?

我正在使用 Spring 3.2.11.RELEASe、JUnit 4.12 和 Mockito 1.10.18。在我的 JUnit 测试中,如何创建 @Autowired spring 服务的间谍(不是模拟,间谍)?以下是服务的声明方式……

@Service("orderService")
public class OrderServiceImpl implements OrderService, InitializingBean 
{

下面是我的 JUnit 测试的设置方式……

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:test-context.xml" })
public class ProcessPDWorkerTest extends AbstractWorkerTest
{
    …    
    @Autowired
    protected OrderService m_orderSvc;

      final OrderService orderSvcSpy = Mockito.spy(getTargetObject(m_orderSvc));
    …
      ReflectionTestUtils.setField(workerObj, "m_orderSvc", orderSvcSpy);

我有……

protected static <T> T getTargetObject(Object proxy)
{
    if ((AopUtils.isJdkDynamicProxy(proxy)))
    {
        try
        {
            return (T) getTargetObject(((Advised) proxy).getTargetSource().getTarget());
        }
        catch (Exception e)
        {
            throw new RuntimeException("Failed to unproxy target.", e);
        }
    }
    return (T) proxy;
}

但我在“Mockito.spy(getTargetObject(m_orderSvc))”行出现以下异常:

java.lang.ClassCastException: org.mainco.subco.myproject.service.OrderServiceImpl cannot be cast to java.lang.Class
    at org.mainco.subco.test.worker.AbstractWorkerTest.createMockOrders(AbstractWorkerTest.java:146)
    at org.mainco.subco.orders.ProcessPDWorkerTest.mockTrainingAssignmentAndOrder(ProcessPDWorkerTest.java:1117)
    at org.mainco.subco.orders.ProcessPDWorkerTest.testCreateTrainingSessionWTrainer(ProcessPDWorkerTest.java:297)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:53)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

适合我:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:test-context.xml" })
public class ProcessPDWorkerTest extends AbstractWorkerTest {

    …    
    @Spy
    @Autowired
    protected OrderService m_orderSvc;

    @Before
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
        ReflectionTestUtils.setField(workerObj, "m_orderSvc", m_orderSvc);
    }

或者,您可以尝试只更改这一行:

 final OrderService orderSvcSpy = Mockito.spy((OrderService)getTargetObject(m_orderSvc));
 …

这应该有效。 Mockito.spy() 是一个重载的静态方法,编译器在编译时选择 Mockito.spy(Class)。