views:

292

answers:

2

Hi Guru,

I know I am bad, I didn't write unit testing and integration testing for my project. But know I am trying to be a good boy and I am having a hard time setting up the environment. So please help. =)

I have my application context under WEB-INF/applicationContext*.xml and in my applicationContext.xml, it has a reference to a properties file for DB user/pass, LDAP host, etc

<bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/spring-config/dev.properties</value>
            </list>
        </property>
    </bean>

and I have another properties for log4j config (diff config for DEV/Staging/Production)

 <!-- log4j setting -->
    <bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
        <property name="targetMethod" value="initLogging" />
        <property name="arguments">
            <list>
                <value>${webapp.root}/${log4j.properties.location}</value>
            </list>
        </property>
    </bean>

Where webapp.root came from web.xml

And now I am trying to throw in a test class.

@Override
    protected String[] getConfigLocations() {
        return new String[]{
            "file:trunk/code/web/WEB-INF/applicationContext.xml",
        };
    }

which references my xml correctly, but all the properties are screwed up.

my question is... is there a way to set up in the test class properly? if not, should I move these classes? and how can I set up log4j if there is a reference to webroot which only exist in a container?! What is the best practice of spring config location?

Please Advise

Thanks

A: 

For unit testing you should not be using the Spring application context. You should be testing all your spring beans and controllers individually as they are the individual units within the system. As they are POJOs it is easy to wire everything together programatically in your test case code. The also solves issues such as the location of the logging properties file as you can programatically specify a different path that does not rely on the webroot property.

The testing chapter in the Spring Reference provides a good overview of how to approach unit and integration testing of applications that use Spring. It also provides details of the various support classes that Spring provides to help with writing unit and integration tests.

Mark
If you don't have an application context for your unit tests, then you can't use spring injection various beans into your test fixtures. Wouldn't you then be missed an important aspect of testing? Or should you do both?
HDave
I would say a test where you don't use an application context is a unit test as your are only testing that individual unit so it doesn't matter that the beans are being programatically injected.If you use an application context I would say that is more like an integration test given you are explicitly testing how everything fits together.
Mark
A: 

This blogpost of mine describes the basic steps to achieve your goal.

Note that the unit tests shouldn't know that you have a webapp-root - they are usually run without any servlet container started. So place the alternative config files in the test packages and try.

Bozho