好吧,一旦我意识到由于有
@Transactional注释而要对课程进行代理,问题的解决方案就变得更加清晰了。我需要做的是拆开代理,然后直接在其上设置模拟对象:
所以在我的
AbstractIntegrationTest:
public final Object unwrapProxy(Object bean) throws Exception { if (AopUtils.isAopProxy(bean) && bean instanceof Advised) { Advised advised = (Advised) bean; bean = advised.getTargetSource().getTarget(); } return bean;}然后在我的
@Before:
@Mockprivate ProcessHelper processHelper;@Beforepublic void setUp() throws Exception { MockitoAnnotations.initMocks(this); IXMLBatchProcessor iXMLBatchProcessor = (IXMLBatchProcessor) unwrapProxy(xmlProcessor); ReflectionTestUtils.setField(iXMLBatchProcessor , "processHelper", processHelper);}@Autowired在注入正确的模拟对象的同时,所有类都保持完整。



