Created
March 20, 2013 02:24
-
-
Save rponte/5201829 to your computer and use it in GitHub Desktop.
JUnit Test Rule example that starts and stops a Spring Context between each test method. This class was written by @alexec.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexecollins.testsupport.rules; | |
import org.junit.rules.TestRule; | |
import org.junit.runner.Description; | |
import org.junit.runners.model.Statement; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; | |
import org.springframework.context.ConfigurableApplicationContext; | |
import org.springframework.context.support.ClassPathXmlApplicationContext; | |
import java.lang.reflect.Field; | |
/** | |
* Creates a context for tests that use Spring. | |
* <p/> | |
* Public fields in the test annotated {@link Autowired} are auto-wired from the context. | |
* | |
* @author alexec (alex.e.c@gmail.com) | |
*/ | |
public class SpringContextRule implements TestRule { | |
/** A list of class-path contexts. */ | |
private final String[] locations; | |
/** The target test. */ | |
private final Object target; | |
public SpringContextRule(String[] locations, Object target) { | |
this.locations = locations; | |
this.target = target; | |
} | |
public Statement apply(final Statement base, Description description) { | |
return new Statement() { | |
@Override | |
public void evaluate() throws Throwable { | |
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext( | |
locations); | |
AutowireCapableBeanFactory beanFactory = context | |
.getAutowireCapableBeanFactory(); | |
/* As this is an example of @Rule, this is a rough hand-rolled injector, | |
* not suitable for production. | |
* More capable ones, that support @Inject, @Qualifier etc. probably exist. */ | |
for (Field f : target.getClass().getFields()) { | |
if (f.isAnnotationPresent(Autowired.class)) { | |
f.set(target, context.getBean(f.getName(), f.getType())); | |
} | |
} | |
context.start(); | |
try { | |
base.evaluate(); | |
} finally { | |
context.close(); | |
} | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More examples,
https://github.com/alexec/test-support/tree/master/src/main/java/com/alexecollins/testsupport/rules