In a project I'm working on, which is using Spring, I see something that really boggles my mind. Apparently there are unit tests that need beans to work and those beans are created from XML files, containing things like:
<bean class="...ListDTO">
<constructor-arg>
<map>
<entry key="use1key">
<value>use1value</value>
</entry>
<entry key="use2key">
<value>use2value</value>
</entry>
</map>
</constructor-arg>
<constructor-arg>
<map>
<entry key="nature1key">
<value>nature1value</value>
</entry>
<entry key="nature2key">
<value>nature2value</value>
</entry>
</map>
</constructor-arg>
<constructor-arg>
<value>false</value>
</constructor-arg>
</bean>
And what did happen? The constructor of the class ...ListDTO changed and hence the bean apparently cannot be created anymore from this (very verbose IMHO) XML.
Can someone explain me why is it good practice (is it really?) to put such thing in an XML instead of Java code? If it was in Java code, as soon as the ...ListDTO would have changed the unit test would have refused to compile (even if the part of the unit test instantiating that bean didn't get executed [for whatever reason]).
Bonus question: is there a way to easily find all these broken "beans in XML" in a project besides running all the unit tests, see which ones are failing and then rinse-and-repeating?
To me it seems a pretty serious issue that you can change a constructor and that the IDE would act as if everything was fine: what is the justification for this? (*)
The minimal bean descriptor xml) is not there to define beans in XML, like in other popular bean containers. Rather, you use this file to enable CDI services for the current bean archive that are difficult to define consistently in Java or which you don't want to define in Java (e.g., to accomodate testing).
From my own experience annotations better than xml configuration. I think in any case you can override xmls and use annotations. Also Spring 4 give us a huge support for annotations, we can override security from xml to annotations e.t.c, so we will have not 100 lines xml but 10 lines Java Code.
The idea behind this is that you keep things that differ between environments (development, testing, production) in a central XML configuration file, and by using a different config file, you switch environments.
However, It's definitely not good practice to use bean configuration to define complex test data structures. Someone probably did that while newly enamoured with dependency injection, just because it's possible.
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