Spring Context中的JMock类模拟

时间:2015-07-13 09:05:52

标签: java spring autowired jmock

我想从Spring XML Configuration中注入JMock模拟bean。 我有以下测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/application-context-test.xml")
public class CorporateInfoServiceImplTest
{
    @Autowired
    private Mockery mockery;

    @Autowired
    private CorporateInfoServiceImpl corporateInfoService;

    @Test
    public void createCorporateInfo() {...}
}

这样的XML配置:

<bean id="mockery" class="org.jmock.Mockery">
  <property name="imposteriser">
    <util:constant static-field="org.jmock.lib.legacy.ClassImposteriser.INSTANCE"/>
  </property>
</bean>

<bean id="createCorporateInfoOperation" class="com.acme.corp.ws.operation.CreateCorporateInfoOperation" factory-bean="mockery" factory-method="mock">
  <constructor-arg value="com.acme.corp.ws.operation.CreateCorporateInfoOperation" type="java.lang.Class" />
</bean>

<bean id="corporateInfoService" class="com.acme.corp.ws.CorporateInfoServiceImpl">
  <property name="createCorporateInfoOperation" ref="createCorporateInfoOperation" />
</bean>

CorporateInfoServiceImpl和CreateCorporateInfoOperation有点:

public class CorporateInfoServiceImpl implements CorporateServiceInterface
{
    @Autowired
    CreateCorporateInfoOperation createCorporateInfoOperation;
    ...
}

@Component
public class CreateCorporateInfoOperation
{

    @Autowired
    CorporateInfoDao corporateInfoDao;
    ...
}

当尝试自动装配CorporateInfoServiceImpl时,我得到以下异常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.acme.corp.dao.CorporateInfoDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

问题是,即使在创建表单工厂方法时,CreateCorporateInfoOperation也会尝试自动装配dao。如何禁用此类自动装配?

赞赏在JMock中注入模拟bean的其他变体。

提前致谢

0 个答案:

没有答案