Ice09

Playing with Spring

Quick Tip: Testing Spring

Posted by ice09 on July 12, 2008

Download Eclipse project

You can read detailed and good written documentation about unit testing with Spring here.
But as always, when combining Spring with Maven using Eclipse, some things have to be considered.

  • Create a “no archetype” Maven project in Eclipse (compare the other posts).
  • Create the test case first (following TDD) with Eclipse. This can be a JUnit 3 or 4 test case.
  • Afterwards, within Eclipse the class generation can easily be done using the Quick Fix funtionality (Strg + 1).

The test case must extends Spring’s AbstractDependencyInjectionSpringContextTests, making it much easier to use Spring in unit tests. It is best combined with the call to setPopulateProtectedVariables(true) in the constructor. Using this feature, Spring can inject Beans into protected fields, avoiding the need for setters in test cases.
By defining getConfigLocations(), special Spring configfiles for unit tests can be chosen. However, using Maven, we are always seperating test and main resources (compare project structure).

Test case:


package test.sample;

import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

public class SpringTest extends AbstractDependencyInjectionSpringContextTests {

	protected BeanUnderTest testbean;

	public SpringTest() {
		setPopulateProtectedVariables(true);
	}

	public void testBeanUnderTest() {
		assertEquals(1, testbean.return1());
	}

	@Override
	protected String[] getConfigLocations() {
		return new String[]{"applicationContext-test.xml"};
	}
}

Class under test (BeanUnderTest):


package test.sample;

public class BeanUnderTest {

	public int return1() {
		return 1;
	}

}

applicationContext-test.xml (currently this is the only config):


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<bean id="testbean" class="test.sample.BeanUnderTest"/>

</beans>

And the Maven POM. It is important to include the Compiler 5 switches if JUnit 4 is used:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>testingsample</groupId>
	<artifactId>testingsample</artifactId>
	<name>testingsample</name>
	<version>0.0.1-SNAPSHOT</version>
	<description />
	<build>
		<finalName>testingsample</finalName>
                <plugins>
                        <plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.5.5</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>2.5.5</version>
		</dependency>
	</dependencies>
</project>

That’s it, Maven, JUnit and Spring can be combined very smoothly and Spring again shows with the AbstractDependencyInjectionSpringContextTests how it provides the useful little glue between these components.

One Response to “Quick Tip: Testing Spring”

  1. [...] – bookmarked by 6 members originally found by crapshaw on 2008-08-31 Quick Tip: Testing Spring http://ice09.wordpress.com/2008/07/12/quick-tip-testing-spring/ – bookmarked by 4 members [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>