ICE09 . playing with java, scala, groovy and spring .

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.

Advertisements

One Response to “Quick Tip: Testing Spring”

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: