|
21 | 21 | import javassist.util.proxy.ProxyFactory;
|
22 | 22 |
|
23 | 23 | public class InjectExtension implements BeforeTestExecutionCallback {
|
| 24 | + private static boolean isEnabled = true; |
| 25 | + |
| 26 | + public static void enable() { |
| 27 | + isEnabled = true; |
| 28 | + } |
| 29 | + |
| 30 | + public static void bypass() { |
| 31 | + isEnabled = false; |
| 32 | + } |
| 33 | + |
| 34 | + public static boolean status() { |
| 35 | + return isEnabled; |
| 36 | + } |
| 37 | + |
24 | 38 | @Override
|
25 | 39 | public void beforeTestExecution(ExtensionContext context) throws Exception {
|
26 | 40 | Object testInstance = context.getTestInstance().get();
|
@@ -80,20 +94,22 @@ private Map<String, List<Field>> createFieldMap(Class<? extends Object> targetCl
|
80 | 94 | }
|
81 | 95 |
|
82 | 96 | private MethodHandler createMethodHandler(final Map<String, Field> injectMap, final Object injectionTarget,
|
83 |
| - final Map<String, List<Field>> fieldMap, final Object testInstance, final Method postConstructMethod) { |
| 97 | + final Map<String, List<Field>> fieldMap, final Object testInstance, final Method postConstructMethod) { |
84 | 98 | return (proxy, invokedMethod, proceedMethod, args) -> {
|
85 | 99 | invokedMethod.setAccessible(true);
|
86 |
| - for (String fieldName : injectMap.keySet()) { |
87 |
| - for (Field targetField : fieldMap.get(fieldName)) { |
88 |
| - Field sourceField = injectMap.get(fieldName); |
89 |
| - sourceField.setAccessible(true); |
90 |
| - targetField.setAccessible(true); |
91 |
| - targetField.set(injectionTarget, sourceField.get(testInstance)); |
| 100 | + if (InjectExtension.isEnabled) { |
| 101 | + for (String fieldName : injectMap.keySet()) { |
| 102 | + for (Field targetField : fieldMap.get(fieldName)) { |
| 103 | + Field sourceField = injectMap.get(fieldName); |
| 104 | + sourceField.setAccessible(true); |
| 105 | + targetField.setAccessible(true); |
| 106 | + targetField.set(injectionTarget, sourceField.get(testInstance)); |
| 107 | + } |
| 108 | + } |
| 109 | + if (postConstructMethod != null) { |
| 110 | + postConstructMethod.setAccessible(true); |
| 111 | + postConstructMethod.invoke(injectionTarget); |
92 | 112 | }
|
93 |
| - } |
94 |
| - if (postConstructMethod != null) { |
95 |
| - postConstructMethod.setAccessible(true); |
96 |
| - postConstructMethod.invoke(injectionTarget); |
97 | 113 | }
|
98 | 114 | try {
|
99 | 115 | return invokedMethod.invoke(injectionTarget, args);
|
|
0 commit comments