Repository navigation
Description
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
- Spring Integration Tests, Part I, Creating Mock Objects (blog)
- EasyMock Objects Injection using Spring and Annotations (blog)
- Injecting Mockito mocks into a Spring bean (discussion at Stack Overflow)
- Mocking Spring beans (discussion at Stack Overflow)
- Springockito (open source project)
- spring-mvc-test with a sprinkle of mockito (blog)
Affects: 3.1 GA
Attachments:
- EasyMockBeanFactory.java (5.21 kB)
Issue Links:
- @Autowired dependencies against bean definitions with type-inspecific factory-methods may fail [SPR-8769] #13412
@Autowireddependencies against bean definitions with type-inspecific factory-methods may fail - Infer return type of parameterized static factory methods [SPR-9493] #14127 Infer return type of parameterized static factory methods
- Return type prediction for generic factory method fails if type conversion of method arguments is necessary [SPR-10411] #15044 Return type prediction for generic factory method fails if type conversion of method arguments is necessary ("is superseded by")
Activity
spring-projects-issues commentedon Feb 17, 2012
Dmitry Katsubo commented
Pull request:
#40
spring-projects-issues commentedon May 16, 2012
Chris Beams commented
Asked Dimitry to separate the single pull request into two and walk through the contributor guidelines for each.
spring-projects-issues commentedon Jun 5, 2012
Dmitry Katsubo commented
Created new pull request:
#89
spring-projects-issues commentedon Jun 10, 2012
Sam Brannen commented
I restructured the description and, more importantly, added the Analysis section.
Sam
spring-projects-issues commentedon Jun 10, 2012
Sam Brannen commented
Dmitry,
Please note that you are referring to what is called a
FactoryBeanin Spring. ABeanFactoryis 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
spring-projects-issues commentedon Jun 10, 2012
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?
Or the following for a standard mock?
Regards,
Sam
spring-projects-issues commentedon Jun 11, 2012
Sam Brannen commented
FYI: if you're watching this issue you may also be interested in watching #14127.
Sam
spring-projects-issues commentedon Jun 13, 2012
Sam Brannen commented
Considering this for 3.2 M2, pending the outcome of #14127.
spring-projects-issues commentedon Jun 14, 2012
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
EasyMockFactoryBeanfor the mock control". I think, you have answered this in description:EasyMockFactoryBeanis needed for Spring core to handle@Autowiredcorrectly by communicating a necessary type. If the sample you've provided in Current Approach works with@Autowired, thenEasyMockFactoryBeanis has no added value.spring-projects-issues commentedon Jun 19, 2012
Sam Brannen commented
Hi Dmitry,
Thanks for the feedback!
Since #14127 has been resolved for Spring 3.2, an
EasyMockFactoryBeanwould no longer be necessary for Spring to properly handle@Autowiredfor 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
@Autowiredas 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
spring-projects-issues commentedon Jun 19, 2012
Sam Brannen commented
In light of #14127, I am resolving this issue as Won't Fix.
Regards,
Sam
spring-projects-issues commentedon Jun 21, 2012
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.