Reputation: 41261
I can unit test most of my Spring classes without needing to do Spring "stuff".
I can unit test @Before advice methods without using Spring too:
Example code:
@Before("execution(* run(..)) && " + "" +
"target(target) && " +
"args(name)")
public void logName(Object target, String name) {
logger.info("{} - run: {}", target, name);
}
Example test:
@Test
public void testLogName() {
aspect.setLogger(mockLogger);
aspect.logName(this,"Barry");
assertTrue(mockLogger.hasLogged("TestAspect - run: Barry"));
}
However @Around advice deals with a ProceedingJoinPoint object:
@Around("com.xyz.myapp.SystemArchitecture.businessService()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
// start stopwatch
Object retVal = pjp.proceed();
// stop stopwatch
return retVal;
}
I don't know how to instantiate a ProceedingJoinPoint object. How do I test this class without starting a whole Spring application context?
Upvotes: 49
Views: 40907
Reputation: 103
If you would like to test specific requirements within an Aspect, you can directly call the Aspect Method and mock the ProceedingJoinPoint
.
Here`s an example.
@ExtendWith(MockitoExtension.class)
class AspectTest {
@InjectMocks
private YourAspect yourAspect;
@Mock
private ProceedingJoinPoint joinPoint;
void should_test_rules_in_aspect(){
final var test = "test";
when(joinPoint.proceed())
.thenReturn(test);
final var returnAfterProfiling = yourAspect.doBasicProfiling(joinPoint);
Assertions.assertEquals(test, returnAfterProfiling );
}
}
Upvotes: 0
Reputation: 41261
You can test a Spring Aspect by creating a proxy programatically:
MyInterface target = new MyClass();
AspectJProxyFactory factory = new AspectJProxyFactory(target);
MyAspect aspect = new MyAspect(arg);
factory.addAspect(aspect);
MyInterface proxy = factory.getProxy();
... then you can call methods on proxy
, and make assertions about aspect
, proxy
and target
.
Upvotes: 110