1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.test.spring; |
17 | |
|
18 | |
import java.lang.reflect.AccessibleObject; |
19 | |
import java.lang.reflect.Field; |
20 | |
|
21 | |
import org.springframework.beans.factory.config.BeanDefinition; |
22 | |
import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
23 | |
import org.springframework.beans.factory.support.RootBeanDefinition; |
24 | |
import org.springframework.context.ApplicationContext; |
25 | |
import org.springframework.test.context.TestContext; |
26 | |
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; |
27 | |
|
28 | 1 | public class DaoTestDependencyInjectorListener extends |
29 | |
DependencyInjectionTestExecutionListener { |
30 | |
|
31 | |
@Override |
32 | |
protected void injectDependencies(TestContext testContext) throws Exception { |
33 | |
|
34 | 2 | ApplicationContext context = testContext.getApplicationContext(); |
35 | |
|
36 | 2 | BeanDefinitionRegistry registry = ((BeanDefinitionRegistry) context); |
37 | |
|
38 | 2 | Field[] fields = testContext.getTestClass().getDeclaredFields(); |
39 | 6 | for (Field f : fields) { |
40 | 4 | Dao a = f.getAnnotation(Dao.class); |
41 | 4 | if (a != null) { |
42 | 2 | Class<?> clazz = Class.forName(a.value()); |
43 | 2 | BeanDefinition definition = new RootBeanDefinition(clazz); |
44 | 2 | registry.registerBeanDefinition(f.getName(), definition); |
45 | 2 | Field.setAccessible(new AccessibleObject[]{f}, true); |
46 | 2 | f.set(testContext.getTestInstance(), testContext |
47 | |
.getApplicationContext().getBean(f.getName())); |
48 | |
} |
49 | |
} |
50 | |
|
51 | 2 | super.injectDependencies(testContext); |
52 | |
|
53 | 2 | } |
54 | |
} |