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 | 0 | public class DaoTestDependencyInjectorListener extends |
29 | |
DependencyInjectionTestExecutionListener { |
30 | |
|
31 | |
@Override |
32 | |
protected void injectDependencies(TestContext testContext) throws Exception { |
33 | |
|
34 | 0 | ApplicationContext context = testContext.getApplicationContext(); |
35 | |
|
36 | 0 | BeanDefinitionRegistry registry = ((BeanDefinitionRegistry) context); |
37 | |
|
38 | 0 | Field[] fields = testContext.getTestClass().getDeclaredFields(); |
39 | 0 | for (Field f : fields) { |
40 | 0 | Dao a = f.getAnnotation(Dao.class); |
41 | 0 | if (a != null) { |
42 | 0 | Class<?> clazz = Class.forName(a.value()); |
43 | 0 | BeanDefinition definition = new RootBeanDefinition(clazz); |
44 | 0 | registry.registerBeanDefinition(f.getName(), definition); |
45 | 0 | Field.setAccessible(new AccessibleObject[]{f}, true); |
46 | 0 | f.set(testContext.getTestInstance(), testContext |
47 | |
.getApplicationContext().getBean(f.getName())); |
48 | |
} |
49 | |
} |
50 | |
|
51 | 0 | super.injectDependencies(testContext); |
52 | |
|
53 | 0 | } |
54 | |
} |