我认为您试图测试的是方面编织和切入点匹配。请注意,这将是一个集成,而不是一个单元测试。如果你真的想对你的方面逻辑进行单元测试,并且因为你已经用“mockito”标记了这个问题,我建议你这样做:编写一个单元测试,模拟方面的连接点,也许还有它的其他参数,如果有的话。下面是一个稍微复杂一些的示例,其中包含一些方面内逻辑:
Java 类的目标方面:
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);
}
}
被测方面:
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();
}
}
运行 Application.main(..)
时的控制台日志:
如您所见,该方面在 11 上传递,否定 -22 并为 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)
方面单元测试:
现在我们真的想验证该方面是否做了它应该做的事情,并涵盖了所有执行路径:
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);
// 'proceed()' is called exactly once
verify(proceedingJoinPoint, times(1)).proceed();
// 'proceed(Object[])' is never called
verify(proceedingJoinPoint, never()).proceed(null);
}
@Test
public void testNegativeNumber() throws Throwable {
sampleAspect.intercept(proceedingJoinPoint, -22);
// 'proceed()' is never called
verify(proceedingJoinPoint, never()).proceed();
// 'proceed(Object[])' is called exactly once
verify(proceedingJoinPoint, times(1)).proceed(new Object[] { 22 });
}
@Test(expected = RuntimeException.class)
public void testPositiveLargeNumber() throws Throwable {
sampleAspect.intercept(proceedingJoinPoint, 333);
}
}
现在运行这个简单的JUnit + Mockito测试,以便单独测试方面逻辑,而不是布线/编织逻辑。对于后者,您需要另一种类型的测试。
P.S.:只有你,我才用了JUnit和Mockito。通常我只使用Spock及其内置的模拟功能。;-)