Spring Framework

From Kb

Jump to: navigation, search

Contact Article Author | Blog of Article Author | FirstPartners.net Home | LinkedIn profile of Author


Contents

Integration testing Junit and Spring

  • For when we want to ensure that our beans and context.xml both align
  • Extend AbstractDependencyInjectionSpringContextTests if you are usign Spring directly.
  • More info in Chapter 8: Testing of the Spring guide.
  • If test resources need to be copied (e.g. to the default directory) see Maven_and_Ant_integration for an example of how to carry this out.
  • resources in 'myproject'/src/test/resources are also on the (maven test) classpath


Adding Spring Mock Using Maven

Ensure you have the Spring Mock Library installed. Using Maven , add the following to your pom.xml

<dependency>
       <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>2.0-m4</version>
      <scope>test</scope>
    </dependency>

Sample Spring JUnit Test

Sample Java code, taking advantage of the Spring autowiring.

import java.io.File;
 
import org.apache.log4j.Logger;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
 
 
public class TestSpringConfig extends
        AbstractDependencyInjectionSpringContextTests {
 
    /**
     * Must overwrite this abstract method to specify the Spring configuration to load for this test fixture
     */
    protected String[] getConfigLocations() {
 
 
        String configBaseLocation = "some-spring-config.xml";
 
        return new String[] { configBaseLocation};
 
 
    }
 
    public void testSomeMethod() {
 
        // Get the bean that we wish to test
        // Hold a copy of the class (TimeShareDaoImpl) that we wish to test.
        Object tmpObject = super.applicationContext
                .getBean("name_of_bean_in_spring_config_file");
 
        // Make sure the bean has been set
        assertNotNull(tmpObject);
 
        // Now Cast the Bean to it's correct class and do something with it (Standard Spring from here on out)
        MyClass dao= (MyClass) tmpObject;
        assertTrue(dao.getHasDBConnection()); //etc
 
    }
 
}


Running Spring from the Command Line

  • Include the following in your code
// Read the configuration file
        ApplicationContext appContext= 
          new FileSystemXmlApplicationContext(
            "spring-config.xml");
 
//get and object from the context
        ISomeObject myMainObject= 
          (ISomeObject ) appContext.getBean("someBeanDefinedInContext");
 
        // Execute any public 
        // method of the bean
        myMainObject.start();


Running a (test) method when the Spring Application is loaded

or when its configuration (context) changes

Probably better to use the above method for testing, as otherwise our tests get mixed up with production code.

  • Configure bean (below) to be loaded by Spring as normal
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
 
 
 
public class MySpringBean implements ApplicationListener {
 
   /**
      * This method will called by Spring on deployment
      * or when the context / configuration changes
      */
     public void onApplicationEvent(ApplicationEvent event)
     {
        //... java code
     }
 
}

Other Spring Framework pages on this Wiki

Personal tools