File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5858 <artifactId >commons-io</artifactId >
5959 <version >2.6</version >
6060 </dependency >
61+ <dependency >
62+ <groupId >org.springframework</groupId >
63+ <artifactId >spring-context</artifactId >
64+ <version >4.3.9.RELEASE</version >
65+ </dependency >
6166 </dependencies >
6267
6368 <build >
Original file line number Diff line number Diff line change 1+ package com .advenoh ;
2+
3+ import com .advenoh .rules .SpringContextRule ;
4+ import org .junit .Rule ;
5+ import org .junit .Test ;
6+ import org .junit .rules .TestRule ;
7+ import org .springframework .beans .factory .annotation .Autowired ;
8+
9+ import static org .assertj .core .api .Assertions .assertThat ;
10+
11+ public class SpringContextRuleTest {
12+
13+ @ Rule
14+ public TestRule contextRule = new SpringContextRule (
15+ new String [] { "testContext.xml" }, this );
16+
17+ @ Autowired
18+ public String bar ;
19+
20+ @ Test
21+ public void testBaz () throws Exception {
22+ assertThat (bar ).isEqualTo ("bar" );
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ package com .advenoh .rules ;
2+
3+ import org .junit .rules .TestRule ;
4+ import org .junit .runner .Description ;
5+ import org .junit .runners .model .Statement ;
6+ import org .springframework .context .ConfigurableApplicationContext ;
7+ import org .springframework .context .support .ClassPathXmlApplicationContext ;
8+
9+ public class SpringContextRule implements TestRule {
10+
11+ /**
12+ * A list of class-path contexts.
13+ */
14+ private final String [] locations ;
15+
16+ /**
17+ * The target test.
18+ */
19+ private final Object target ;
20+
21+ public SpringContextRule (String [] locations , Object target ) {
22+ this .locations = locations ;
23+ this .target = target ;
24+ }
25+
26+ public Statement apply (final Statement base , Description description ) {
27+ return new Statement () {
28+ @ Override
29+ public void evaluate () throws Throwable {
30+ ConfigurableApplicationContext context = new ClassPathXmlApplicationContext (
31+ locations );
32+ context .getAutowireCapableBeanFactory ().autowireBean (target );
33+ context .start ();
34+ try {
35+ base .evaluate ();
36+ } finally {
37+ context .close ();
38+ }
39+ }
40+ };
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <beans xmlns =" http://www.springframework.org/schema/beans"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xmlns : context =" http://www.springframework.org/schema/context"
5+ xsi : schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" >
6+
7+ <context : annotation-config />
8+
9+ <bean id =" bar" class =" java.lang.String" >
10+ <constructor-arg value =" bar" />
11+ </bean >
12+
13+ </beans >
You can’t perform that action at this time.
0 commit comments