Skip to content

Commit 4a1f28d

Browse files
authoredMar 2, 2023
Merge pull request #7 from atsmalec/issues/6_enable-disable-injection
Fix #6 - Allows enabling/disabling of injection, also allows to query status of injection.
2 parents 6b80f1f + 5f6c4d3 commit 4a1f28d

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed
 

‎README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,24 @@ class MyControllerTest {
129129

130130
Ref: https://docs.oracle.com/javaee/7/api/javax/annotation/PostConstruct.html
131131

132+
## Enabling/Disabling Injection Behavior
133+
134+
The InjectExtension provides methods to turn on and off the behavior as well as query the status of injection.
135+
136+
```
137+
InjectExtension.enable();
138+
InjectExtension.bypass();
139+
InjectExtension.status(); // returns true if enabled
140+
```
141+
142+
It's recommended that if you're using the above APIs that you reset the Injector status between tests.
143+
144+
```
145+
@AfterEach
146+
void afterEach() {
147+
InjectExtension.enable();
148+
}
149+
```
132150

133151
## License
134152

@@ -154,7 +172,7 @@ Maven Coordinates:
154172
<dependency>
155173
<groupId>com.github.exabrial</groupId>
156174
<artifactId>mockito-object-injection</artifactId>
157-
<version>2.1.0</version>
175+
<version>2.2.0</version>
158176
<scope>test</scope>
159177
</dependency>
160178
```

‎pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.github.exabrial</groupId>
55
<artifactId>mockito-object-injection</artifactId>
6-
<version>2.1.1-SNAPSHOT</version>
6+
<version>2.2.0-SNAPSHOT</version>
77
<packaging>jar</packaging>
88
<description>Mockito Object Injection</description>
99
<name>${project.artifactId}</name>

‎src/main/java/com/github/exabrial/junit5/injectmap/InjectExtension.java

+27-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@
2121
import javassist.util.proxy.ProxyFactory;
2222

2323
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+
2438
@Override
2539
public void beforeTestExecution(ExtensionContext context) throws Exception {
2640
Object testInstance = context.getTestInstance().get();
@@ -80,20 +94,22 @@ private Map<String, List<Field>> createFieldMap(Class<? extends Object> targetCl
8094
}
8195

8296
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) {
8498
return (proxy, invokedMethod, proceedMethod, args) -> {
8599
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);
92112
}
93-
}
94-
if (postConstructMethod != null) {
95-
postConstructMethod.setAccessible(true);
96-
postConstructMethod.invoke(injectionTarget);
97113
}
98114
try {
99115
return invokedMethod.invoke(injectionTarget, args);

0 commit comments

Comments
 (0)