t, times(2)).changeIncrementAndGet(any()),使用Mockito测试FooAspect.changeIncrementAndGet是否被调用了两次
上面的测试代码测试的是FooAspect的行为,而不是FooServiceImpl的行为,这种测试方法更为通用。
例子3:Spring Boot的例子
上面两个例子使用的是Spring Testing工具,下面举例Spring Boot Testing工具如何测AOP(其实大同小异):
SpringBootAopTest:
@SpringBootTest(classes = { SpringBootAopTest.class, AopConfig.class })
@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)
public class SpringBootAopTest extends AbstractTestNGSpringContextTests {
@SpyBean
private FooAspect fooAspect;
@Autowired
private FooService fooService;
@Test
public void testFooService() {
// ...
verify(fooAspect, times(2)).changeIncrementAndGet(any());
}
}
参考文档