Lets have a look the following JUnit test case. Notice the sleek and simple looks of this piece of code.
package com.chiradip.rd;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/META-INF/spring/module-context.xml" })
@TestExecutionListeners(value = DependencyInjectionTestExecutionListener.class)
public class SimpleBeanTest {
@Autowired
private SimpleBean simpleBean;
@Test
public void testJustAMethod() {
simpleBean.justAMethod();
}
}
As you can see we are testing a spring bean - named SimpleBean
. This is autowired. Pretty simple and easy so far. As you can see it is a clutter free test case. SimpleBean.java
is a very simple spring bean - just as below.
package com.chiradip.rd;
import org.springframework.stereotype.Component;
@Component
public class SimpleBean {
public void justAMethod() {
System.out.println("test");
}
}
N.B.: I am just using annotation based bean definition and detection instead of using XML. @Component
annotation says SimpleBean
is a spring bean.
Just for those want to have a look into my spring configuration - it goes below.
The above configuration file is given just to show how @Component annotation works. Coming back to the the test case. It looks simple and sexy - and has 3 annotations on the test class "SimpleBeanTest". Most important annotation. as you can guess, is the second one @ContextConfiguration
. locations
attribute there overrides the default location and specifies the location of the spring configuration file(s) those need to be loaded into context during test. You got it right - it loads the the bean definitions and makes it available in the current context so that the test methods can retrieve those defined beans - including using @Autowired
.
Lets now see the first annotation @RunWith
. "By annotating test classes with @RunWith(SpringJUnit4ClassRunner.class), developers can implement standard JUnit 4.5+ unit and integration tests and simultaneously reap the benefits of the TestContext framework such as support for loading application contexts, dependency injection of test instances, transactional test method execution, and so on." It says it all. Now the 3rd (last) annotation @TestExecutionListeners
is optional and not always used. For example in our example its doing nothing. This is very instrumental to control (and/or capture) behavior of the test case under execution. Find more here Springframework 3.0.X testing.
In case you want to look at my pom.xml
it goes here below. Note - I used all the artifacts from springsource bundle repository. These are all OSGi-fied. Though OSGi benefits and features are not used in this example - it is ready to be used in OSGi context. Take google's help if you need to know more about OSGi - the future of the the application backbone.
4.0.0
com.chiradip.rd
space
1.0-SNAPSHOT
jar
space
http://www.frameworkfactory.org
UTF-8
3.0.3.RELEASE
org.junit
com.springsource.org.junit
4.8.1
test
org.springframework
org.springframework.core
${spring.version}
org.springframework
org.springframework.beans
${spring.version}
org.springframework
org.springframework.context
${spring.version}
org.springframework
org.springframework.context.support
${spring.version}
org.springframework
org.springframework.aspects
${spring.version}
org.springframework
org.springframework.aop
${spring.version}
org.springframework
org.springframework.instrument
${spring.version}
org.springframework
org.springframework.expression
${spring.version}
org.springframework
org.springframework.test
${spring.version}
test
org.apache.commons
com.springsource.org.apache.commons.logging
1.1.1
org.aspectj
com.springsource.org.aspectj.runtime
1.6.8.RELEASE
org.apache.maven.plugins
maven-surefire-plugin
**/*Test.java
**/Abstract*.java
org.junit:com.springsource.org.junit
com.springsource.repository.bundles.release
SpringSource Enterprise Bundle Repository - SpringSource Bundle Releases
http://repository.springsource.com/maven/bundles/release
com.springsource.repository.bundles.external
SpringSource Enterprise Bundle Repository - External Bundle Releases
http://repository.springsource.com/maven/bundles/external