Skip to content

Commit 99bf88f

Browse files
author
Frank Oh
committed
added springcontextrule
1 parent df5c40c commit 99bf88f

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

junit-rule/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
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>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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>

0 commit comments

Comments
 (0)