Question

I am not much of a Spring expert, but was given a legacy system with a huge context file (not separated into modules).

I want to add some unit tests - that validates different parts of the system, with the actual production configuration. I started using the ClassPathXmlApplicationContext/FileSystemXmlApplicationContext classes to load the context, however - that takes forever. Is it possible to load only parts of the context file (recursively) without the need to separate the original file into modules?

Update: I'll just post here my implementation of Ralph's solution using maven: my pom.xml:

      <plugin>
        <groupId>com.google.code.maven-config-processor-plugin</groupId>
        <artifactId>maven-config-processor-plugin</artifactId>
        <version>2.0</version>
        <configuration>
            <namespaceContexts>
                <s>http://www.springframework.org/schema/beans</s>
            </namespaceContexts>
            <transformations>
                <transformation>
                    <input>context.xml</input>
                    <output>context-test.xml</output>
                    <config>test-context-transformation.xml</config>
                </transformation>
            </transformations>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>process</goal>
                </goals>
                <phase>test</phase>
            </execution>
        </executions>
    </plugin>

my test-context-transformation.xml:

<processor>
    <add>
        <name>/s:beans</name>
        <value>
            <![CDATA[
                default-lazy-init="true"
            ]]>
        </value>
    </add>
</processor>
Was it helpful?

Solution

I guess it does not work out of the box. But you can try this (it is just an idea, I don't know if it works)

Spring support so called lazy initialization the idea is to add this to all the beans. I can imagine two ways.

  • A simple tool that create an copy of the orignal configuration xml file and add default-lazy-init="true" the container level beans (with s) declaration.
  • Try do do it programmatic. With a Bean Post Processor, or try to "inject" the default-lazy-init="true" configuration programmatic

OTHER TIPS

If you are trying to run "unit" tests, you will not require the full application context at all. Just instantiate the class you want to test (and maybe its collaborators, though mocking may be a better option) and off you go. Unit tests should concentrate on single components in isolation - otherwise they are not unit tests.

If you are trying to run a full integration test by creating the complete object hierarchy defined in your application context, then it may be easiest by first refactoring your context and splitting it into modules - as you were suggesting already.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top