'Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available"
I'm using org.springframework.boot version '2.3.3.RELEASE'.
When I'm running Integration tests on ProcessService class, I get the error:
java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available, and ensure that AopContext.currentProxy() is invoked in the same thread as the AOP invocation context.
at org.springframework.aop.framework.AopContext.currentProxy(AopContext.java:69)
This is the main class:
@EnableRetry
@EnableScheduling
@SpringBootApplication
@EnableRedNotificationService
@EnableAspectJAutoProxy(exposeProxy = true)
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
Service class getting the error:
@Slf4j
@Service
public class ProcessService {
@Retryable(maxAttemptsExpression = "${api.retry.limit}", backoff = @Backoff(delayExpression = "${api.retry.max-interval}",
multiplierExpression = "${api.retry.delay-multiplier}", maxDelayExpression = "${api.retry.max-delay}"))
public void triggerJob(Parameters...) throws Exception {
// Some code
method1();
// some code
}
public JobResult method1(Parameters.. ) {
ProcessService processService = (ProcessService) AopContext.currentProxy();
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<JobResult> task = executorService.submit(() -> processService.method2(Parameters..));
// some code
}
@Retryable(maxAttemptsExpression = "${api.retry.limit}", backoff = @Backoff(delayExpression = "${api.retry.max-interval}",
multiplierExpression = "${api.retry.delay-multiplier}", maxDelayExpression = "${api.retry.max-delay}"))
JobResult method2(Parameters.. ) {
// Some code
}
}
Any suggestions on how to fix this? How should I mock the AopContext.currentProxy() call in method1 to return an object of ProcessService class?
Currently, while test execution, the executon is stepping inside currentProxy() method of org.springframework.aop.framework.AopContext class and returning null.
public static Object currentProxy() throws IllegalStateException {
Object proxy = currentProxy.get();
if (proxy == null) {
throw new IllegalStateException(
"Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available, and " +
"ensure that AopContext.currentProxy() is invoked in the same thread as the AOP invocation context.");
}
return proxy;
}
Solution 1:[1]
In the Integration tests, I had to create a Mock static of AopContext and return the class.
@Mock
private MockedStatic<AopContext> aopContextMockedStatic;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
processService = spy(new ProcessService(InputPraramters...));
aopContextMockedStatic.when(AopContext::currentProxy).thenReturn(processService);
}
@AfterEach
void tearDownStatic() {
aopContextMockedStatic.close();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | just_curious |
