In Guice, I have a ProductionModule with my bindings. In my tests, I can load that PLUS a module that overrides a few of the production bindings with mock objects. How do I do such a thing in spring….
For example, load production-spring.xml in test files and then have the test load in test-spring.xml which would ONLY override some of the bindings in production-spring.xml
This tests the integration and make sure changes in production-spring.xml don’t break things. These are more automated integration tests then testing a unit and work extremely well.
You can override beans by listing multiple xml files. Beans in the later files will override those loaded before.
So in this case testDB.xml overrides the DataSource configured in prodDb.xml. This applies even if you don’t use the SpringJUnit4ClassRunner as well:
Using a tool like Constretto you can do the same with annotated beans:
Now, if you run a test with the
@Environment("test")annotation on the class, the FakeFooService will be used.