Reputation: 450
I have a Java 7/Spring MVC project I am working on right now. I have a class A. I want to test A's logic so I created a mock class called B, that extends A and picks up its protected methods.
I have an application-config.xml file and a unit-testing-config.xml In the app-config I create a bean for A and set the beans id. In the unit-testing-config I override that definition with B.
I have logged which constructor gets called and it is A. But when I remove the extends part of B, and rerun, spring creates an instance of B. Is there a way to tell spring explicitly that I want the child to be instantiated. My definition looks like this:
<bean id="something" class="com.site.namespace.package.MockA" autowire="byName" />
Upvotes: 2
Views: 1174
Reputation: 719
Spring Re-Inject can inject mocks without modification of XML files.
Upvotes: 0
Reputation: 7735
As I understood, you have same id for the bean in application & unit-testing-config. That means that the problem with the load, is most likely related to the order in which your xml configuration files loaded. If in first case application-config.xml was loaded last, A would override unit-testing-config.xml B definition, and otherwise. So you need to be sure, that you load your xml's in the same order, to get desired effect.
Have you worked with Spring profiles: https://spring.io/blog/2011/02/14/spring-3-1-m1-introducing-profile/ This is preferable way to do this kind of testing, with separate instances for testing and production environment. I would switch to profiles, and keep configurations in one place.
Upvotes: 1