Skip to content

Introduce FactoryBean to create mock beans with EasyMock [SPR-9130] #13769

Closed

Description

@spring-projects-issues

Dmitry Katsubo opened SPR-9130 and commented

Problem Area

I would like to have a myService definition in both production and test application contexts so that the service is further autowired into other services. The test application context should create a mock version of the bean, e.g. by means of IMocksControl.createMock().

Current Approach

The test application context defines:

<!-- Common for all mocks: -->
<bean name="mockCtrl" class="org.easymock.EasyMock" factory-method="createStrictControl" />

<bean name="myService" factory-bean="mockCtrl" factory-method="createMock">
	<constructor-arg value="org.company.api.MyService" />
</bean>
<!-- ...  other mock definitions ... -->

The drawback of this approach is that the given service can no longer be autowired with @Autowired MyService myService, but it can be injected with @Resource.

Analysis

The reason that the mock cannot be autowired by type is that the parametrized return type of the method configured via factory-method is not currently honored by Spring in all cases (i.e., depending on the order in which beans are defined in the context). In other words, the returned bean is not a candidate for autowiring by type for the type T (i.e., org.company.api.MyService in this example).

For a succinct discussion of this behavior, see the Explanation and Alternative solution sections of the Spring Integration Tests, Part I, Creating Mock Objects blog post.

Note that this is a general limitation of using factory-method in Spring; this is not specific to testing or mocks.

Furthermore, the same naturally applies to other mocking frameworks such as Mockito.

Proposal

The solution is to use a specific FactoryBean:

<bean name="myService" class="org.springframework.test.context.support.EasyMockFactoryBean"
	p:mockInterface="org.company.api.MyService" />

Related Resources


Affects: 3.1 GA

Attachments:

Issue Links:

Activity

  1. spring-projects-issues commented on Feb 17, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Dmitry Katsubo commented

    Pull request:
    #40

  2. spring-projects-issues commented on May 16, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Asked Dimitry to separate the single pull request into two and walk through the contributor guidelines for each.

  3. spring-projects-issues commented on Jun 5, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Dmitry Katsubo commented

    Created new pull request:
    #89

  4. spring-projects-issues commented on Jun 10, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    I restructured the description and, more importantly, added the Analysis section.

    Sam

  5. spring-projects-issues commented on Jun 10, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Dmitry,

    Please note that you are referring to what is called a FactoryBean in Spring. A BeanFactory is something else in Spring.

    So please rename your implementation to EasyMockFactoryBean. (note that I've already changed the proposed class name in the description)

    Cheers,

    Sam

  6. spring-projects-issues commented on Jun 10, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Dmitry,

    Just to clarify...

    Why do you create a separate bean for the mock control?

    Why not just do the following for a strict mock?

    <bean id="myService" class="org.easymock.EasyMock" factory-method="createStrictMock">
        <constructor-arg value="org.company.api.MyService" />
    </bean>

    Or the following for a standard mock?

    <bean id="myService" class="org.easymock.EasyMock" factory-method="createMock">
        <constructor-arg value="org.company.api.MyService" />
    </bean>

    Regards,

    Sam

  7. spring-projects-issues commented on Jun 11, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    FYI: if you're watching this issue you may also be interested in watching #14127.

    Sam

  8. spring-projects-issues commented on Jun 13, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Considering this for 3.2 M2, pending the outcome of #14127.

  9. spring-projects-issues commented on Jun 14, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Dmitry Katsubo commented

    Sam, thanks for fantastic problem description: that is indeed the reason to use EasyMockFactoryBean.

    Why do you create a separate bean for the mock control?

    I am not sure what do you refer here as "bean", I suppose your question is "Why do you create a separate EasyMockFactoryBean for the mock control". I think, you have answered this in description: EasyMockFactoryBean is needed for Spring core to handle @Autowired correctly by communicating a necessary type. If the sample you've provided in Current Approach works with @Autowired, then EasyMockFactoryBean is has no added value.

  10. spring-projects-issues commented on Jun 19, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Hi Dmitry,

    Thanks for the feedback!

    Since #14127 has been resolved for Spring 3.2, an EasyMockFactoryBean would no longer be necessary for Spring to properly handle @Autowired for beans created via such factory methods.

    In other words, with Spring 3.2 and higher, the sample provided in the Current Approach section should work with @Autowired as is.

    I encourage you to try this out in a nightly build or the upcoming Spring 3.2 M2 release, and please provide feedback in case you find it does not suit your needs.

    Regards,

    Sam

  11. spring-projects-issues commented on Jun 19, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    In light of #14127, I am resolving this issue as Won't Fix.

    Regards,

    Sam

  12. spring-projects-issues commented on Jun 21, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Dmitry Katsubo commented

    Sam, I fully agree with you: #14127 will make life will be easier and happier without EasyMockFactoryBean. Looking forward for 3.2M2.

  13. added
    status: declined
    in: test
    type: enhancement
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: teststatus: declinedtype: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions