I have a Spring-enabled JUnit Test. It loads my default applicationContext. Now I want to replace a single bean for my test, i.e. entityManager with testEntityManager.
I can imagine three possible ways, which don't seem very elegant:
Is there a best practice how to do it right?
Background to this question is: I really only want to replace the objects close at the boundary (DB, Webservices, etc)
Yours Sincerely
EDIT: I have solved it this way now: I added a properties file to my classpath and test classpath and used a Spring alias in conjunction with a property placeholder. This way I was able to wire my beans to a different implementation in the tests.
@Before – Run before @Test, public void. @After – Run after @Test, public void.
Which of the following is correct about Test Suite in JUnit? Explanation: Test suite allows us to aggregate a few unit test cases from multiple classes in one place and run them together. @RunWith and @Suite annotation are used to run the suite test.
Spring allows you to override bean definitions, when you are loading contexts from multiple locations.
So you don't necessarily need to to split the context "/applicationContext.xml". Instead have an additional application context for test "/applicationContext-test.xml", where you override the bean you need. Then pull in both configurations and have the bean in the test configuration override the bean in the production configuration.
@ContextConfiguration({"/applicationContext.xml", "/applicationContext-test.xml"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With