I think you are trying to test this aspect weaving and pointcut mapping. Note that this will be an integration, not a unit test. If you really want the unit test of the logic of your aspect, and because in any case you noted the question "mockito", I suggest you do just that: Write a unit test and make fun of the connection point of the aspect and, possibly, its other parameters, if any. Here is a slightly more complex example with some intra-aspect logic:
The Java class should be aimed at the aspect:
package de.scrum_master.app; public class Application { public static void main(String[] args) { new Application().doSomething(11); new Application().doSomething(-22); new Application().doSomething(333); } public void doSomething(int number) { System.out.println("Doing something with number " + number); } }
Aspect under the test:
package de.scrum_master.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class SampleAspect { @Around("execution(* doSomething(int)) && args(number)") public Object intercept(final ProceedingJoinPoint thisJoinPoint, int number) throws Throwable { System.out.println(thisJoinPoint + " -> " + number); if (number < 0) return thisJoinPoint.proceed(new Object[] { -number }); if (number > 99) throw new RuntimeException("oops"); return thisJoinPoint.proceed(); } }
Console log when running Application.main(..)
:
As you can see, the aspect goes to 11, negates -22 and throws an exception for 333:
execution(void de.scrum_master.app.Application.doSomething(int)) -> 11 Doing something with number 11 execution(void de.scrum_master.app.Application.doSomething(int)) -> -22 Doing something with number 22 execution(void de.scrum_master.app.Application.doSomething(int)) -> 333 Exception in thread "main" java.lang.RuntimeException: oops at de.scrum_master.aspect.SampleAspect.intercept(SampleAspect.aj:15) at de.scrum_master.app.Application.doSomething(Application.java:10) at de.scrum_master.app.Application.main(Application.java:7)
Unit test for aspect:
Now we really want to make sure that the aspect does what it needs to and covers all the execution paths:
package de.scrum_master.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.junit.Rule; import org.junit.Test; import org.mockito.Mock; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; import static org.mockito.Mockito.*; public class SampleAspectTest { @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); @Mock private ProceedingJoinPoint proceedingJoinPoint; private SampleAspect sampleAspect = new SampleAspect(); @Test public void testPositiveSmallNumber() throws Throwable { sampleAspect.intercept(proceedingJoinPoint, 11);
Now run this simple JUnit + Mockito test to test the isolated logic aspect, not the wiring / weaving logic. For the latter, you will need a different type of test.
PS: For you, I used JUnit and Mockito. I usually just use Spock and its built-in mocking capabilities. ;-)