-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
maintain original aspect functionality
- Loading branch information
1 parent
5735f80
commit 4260471
Showing
5 changed files
with
110 additions
and
40 deletions.
There are no files selected for viewing
20 changes: 10 additions & 10 deletions
20
src/main/java/com/teketik/test/mockinbean/BeanFieldState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
package com.teketik.test.mockinbean; | ||
|
||
import org.springframework.test.context.TestContext; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
class BeanFieldState extends FieldState { | ||
|
||
private Object bean; | ||
final Object bean; | ||
|
||
private Object originalValue; | ||
final Object originalValue; | ||
|
||
private Object mockableValue; | ||
|
||
public BeanFieldState(Object bean, Field field, Object originalValue, Object mockableValue, Definition definition) { | ||
public BeanFieldState(Object bean, Field field, Object originalValue, Definition definition) { | ||
super(field, definition); | ||
this.bean = bean; | ||
this.originalValue = originalValue; | ||
this.mockableValue = mockableValue; | ||
} | ||
|
||
@Override | ||
public Object resolveTarget(TestContext testContext) { | ||
return bean; | ||
} | ||
|
||
public Object getMockableValue() { | ||
return mockableValue; | ||
public void rollback(TestContext testContext) { | ||
final Object target = resolveTarget(testContext); | ||
ReflectionUtils.setField(field, target, originalValue); | ||
} | ||
|
||
public Object getOriginalValue() { | ||
return originalValue; | ||
public Object createMockOrSpy() { | ||
return definition.create(originalValue); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/com/teketik/test/mockinbean/ProxiedBeanFieldState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.teketik.test.mockinbean; | ||
|
||
import org.springframework.aop.TargetSource; | ||
import org.springframework.test.context.TestContext; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* Special kind of {@link BeanFieldState} handling proxied beans (like aspects).<br> | ||
* The mock is not injected into the <code>field</code> but into the <code>target</code> of its {@link TargetSource}. | ||
* @author Antoine Meyer | ||
*/ | ||
class ProxiedBeanFieldState extends BeanFieldState { | ||
|
||
private static void setTargetSourceValue(TargetSource targetSource, Object value) { | ||
ReflectionTestUtils.setField(targetSource, "target", value); | ||
} | ||
|
||
final TargetSource proxyTargetSource; | ||
|
||
final Object proxyTargetOriginalValue; | ||
|
||
public ProxiedBeanFieldState(Object inBean, Field beanField, Object beanFieldValue, TargetSource proxyTargetSource, Definition definition) throws Exception { | ||
super(inBean, beanField, beanFieldValue, definition); | ||
this.proxyTargetSource = proxyTargetSource; | ||
this.proxyTargetOriginalValue = proxyTargetSource.getTarget(); | ||
} | ||
|
||
@Override | ||
public void rollback(TestContext testContext) { | ||
setTargetSourceValue(proxyTargetSource, proxyTargetOriginalValue); | ||
} | ||
|
||
@Override | ||
public Object createMockOrSpy() { | ||
Object applicableMockOrSpy = definition.create(proxyTargetOriginalValue); | ||
setTargetSourceValue(proxyTargetSource, applicableMockOrSpy); | ||
return originalValue; //the 'mock or spy' to operate for proxied beans are the actual proxy | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters