0

I have added a custom annotation on a method which is present inside a controller class. I have implemented Aspect class which is considering custom annotation as a point-cut. Once the execution reaches to custom annotation ,it calls the interceptor method which is present inside the class .This interceptor method contains a logic which calls the database to save the data.

While writing testcases for controller class, how can I mock above mentioned interceptor method so that I can resist the database call ?

Note: Inside interceptor method I am calling a method of another class which is the implementation of HandlerInterceptor

@Aspect
class SampleAspect {
  @Around("execution(@Xyz)")
  public Object interceptor(ProceedingJoinPoint jointPoint) {
    // database logic
  }
}
@RestController 
class SampleController {
  @GetMapping("/{id}")
  @Xyz
  public String getdata(String id) {
    return "hello";
  }
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Welcome to SO. I added syntax highlighting, split your example to one code block per file and fixed the indentation. Please give your question a bit more love next time. It would also be helpful to see your test code. Then community members could help you fix your test instead of writing the whole test for you, which is not what Stack Overflow was made for. When asking questions here, ideally provide a full [MCVE](https://stackoverflow.com/help/mcve) reproduding your problem. BTW, the pointcut `execution(@Xyz)` is invalid. Please do not post pseudo code with syntax errors. Thank you. – kriegaex Nov 22 '21 at 15:23

1 Answers1

0

I wrote two answers which might be of interest to you:

Please start from there and let me know if you have any follow-up problems, updating your own question and showing exactly what you are trying to do.

kriegaex
  • 63,017
  • 15
  • 111
  • 202