简体   繁体   English

Mockito-春天的MockSettings

[英]Mockito - MockSettings with spring

I'm trying to setup mocked object (using Mockito ) via spring bean configuration, however I don't know how to setup MockSettings for that object. 我试图通过spring bean配置来设置MockSettings对象(使用Mockito ),但是我不知道如何为该对象设置MockSettings Especially I would like to set up object serializable. 特别是我想设置可序列化的对象。

Programatically it is possible by: 通过编程可以通过以下方式实现:

Object serializableMock = mock(Object.class, withSettings().serializable());
<bean id="object" name="object" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="object" />
    <constructor-arg value="org.mockito.MockSettings"> ???  </constructor-arg>
</bean>

Can somebody guide me how to do that ? 有人可以指导我该怎么做吗? Thanks in advance 提前致谢

BTW : I want to use pure XML configuration. 顺便说一句:我想使用纯XML配置。

Only via XML, you can't. 仅通过XML不能。

Actually I wouldn't recommend you to have the usual practice of using Mockito mocks in spring for tests. 实际上,我不建议您使用在春季使用Mockito模拟进行测试的通常做法。 Here's why : 原因如下:

  • Usually when Unit Testing , you want to test one class in isolation, it's unit testing after all, so Spring DI isn't necessary at all in this case. 通常,在单元测试时 ,您想单独测试一个类,毕竟它是单元测试,因此在这种情况下,根本不需要Spring DI。 You just inject the collaborators of your test subject yourself or maybe via the handy @InjectMock annotation. 您可以自己注入测试对象的协作者,也可以通过方便的@InjectMock注释@InjectMock

  • If however you need to test things with another system like a DAO with a Database, then indeed you probably need Spring wiring to connect to either the real DB or some in memory DB like H2. 但是,如果您需要使用带有数据库的DAO之类的其他系统进行测试,那么实际上您可能需要Spring接线才能连接到实际的数据库或内存数据库中的某些数据库(例如H2)。 But in this case you are crafting an Integration Test . 但是在这种情况下,您正在设计一个集成测试 And you most probably don't need mocks in this case. 在这种情况下,您很可能不需要模拟。

That said, you might have specific needs and the above point could be irrelevant in your specific bounded context . 就是说,您可能有特定的需求,而以上几点在您的特定有限上下文中可能无关紧要。 But then again, in my opinion if it's specific I don't think it's overkill to craft yourself a simple MockSettings factory bean (that could even be configurable). 但是话又说回来,我认为如果是特定的话,我认为自己制作一个简单的MockSettings 工厂bean (甚至可以配置)也不为过。

Eg you could write this once and for all in a technical module of your application : 例如,您可以在应用程序的技术模块中一劳永逸地编写此代码:

public class SpringMockSettingsFactoryBean extends AbstractFactoryBean<MockSettings> {
    @Override public Class<Multimap> getObjectType() {
        return MockSettings.class;
    }

    @Override protected Multimap<String, String> createInstance() throws Exception {
        // ... your code

        return mockSettings;
    }
}

There's a project springockito on bitbucket that tries to have a mockito focused namespace in spring. bitbucket上有一个项目springockito ,它试图在Spring中拥有一个以Mockito为主的命名空间。 I don't think the project can do that, but the framework's author might be interested to implement the feature. 我认为该项目无法做到这一点,但是框架的作者可能会对实现该功能感兴趣。

Hope that helps. 希望能有所帮助。

yes, you can do it via XML 是的,您可以通过XML来实现

 
 
   <bean id="mockSettings" class="org.mockito.Mockito" factory-method="withSettings" init-     method="verboseLogging">

    </bean>
    <bean id="sqsHelper" class="org.mockito.Mockito" factory-method="mock">
        <constructor-arg value="com.elsevier.vtw.aws.helper.SQSHelper"/>
        <constructor-arg ref="mockSettings" />

    </bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM