5

I'm trying to setup some best practices how to organize your Spring configuration together with testing so it doesn't become your nightmare.

My 2 main goals are:

  1. Minimize XML configuration. I'm most concerned about a lot of XML files for test purposes.
  2. In tests use default configuration as a basis (same as in a production), so you mock what you need implicitly.

Best practices are:

  1. Use annotations autowiring as a default configuration for dependency injection. In a default Spring XML configuration there are no bean definitions for services, resources etc.
  2. Move all context:component-scan into a applicatonConfig-main.xml so it can be mocked in tests. There should be no other configuration in this file.
  3. Move all properties that can differ between environments from XML to properties file that can be accessed using property file placeholder together with spring.active.profiles property as described here: http://maciejwalkowiak.pl/blog/2012/03/27/spring-3-1-profiles-and-tomcat-configuration/
  4. Move all Spring configuration files to one folder. For example if your app consists of projects like persistence, model etc. All XML config files dedicated for these projects are moved to web app project/

For tests:

  1. Use a default Spring configuration for setting up app context and set mocks programmatically in test setup rather then using separate Spring configuration. In this approach we can still use default Spring configuration, no need to maintain additional Spring config file.
  2. If 1 is difficult for some reasons, write a separate Spring XML file with own set and mocks definition. What is important here is that this file does nothing more than mock settings and the rest of properties is the same as in default configuration.

There are a lot of tutorials about Spring unit testing, but when it comes to the real life maintenance problem always appear.

What do you think about that?

czajek
  • 714
  • 1
  • 9
  • 23
  • One of the useful things is to put common configurations in one file and import it as needed: [Composing XML-based configuration metadata](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-xml-import) – informatik01 Nov 15 '13 at 12:39
  • I want to do that but rather for separation reason than reuse as a common file. What I'm trying to do is to have just one spring config file. Differences between environments I want to solve by properties file and spring profiles. – czajek Nov 15 '13 at 12:43
  • Prefer JavaConfig everywhere, modularize configuration, use Environment for properties, inject by constructor only etc. – MariuszS Nov 15 '13 at 12:43
  • 1
    One big config file is bad idea, it is much simpler and faster to test part of your application using small configuration module. – MariuszS Nov 15 '13 at 12:44
  • I agree with others that it is not a good idea to make one big config file. It's much more convenient and intuitive to split Spring configurations. Check out the last picture (showing Spring Tool Suite) in my following answer, it shows how I split Spring configuration files in one of my previous projects: [Webapp file organization convention (development structure)](http://stackoverflow.com/a/14200520/814702). Of course it is a matter of preference... – informatik01 Nov 15 '13 at 12:50
  • Hi MariuszS. I want to have several spring config files but only for separation. In a test I want to setup app context with the main Spring file that imports ALL other Spring XML files. I want to keep my XML configuration pretty flat. So there is a main XML file which imports all persistance.xml, service.xml etc. In your approach I will have to import persistance.xml in service.xml file when writing test for service layer. – czajek Nov 15 '13 at 12:53
  • Ok, I see that point 2 was not displaying correct. It should be: 2. Move all context:component-scan into a applicatonConfig-main.xml so it can be mocked in tests. There should be no other configuration in this file. – czajek Nov 15 '13 at 12:57

0 Answers0