验证测试中调用点数
我有一个虚拟项目,我尝试弄清楚如何测试要触发的点数。
我的项目由1个方面的bean组成,只在调用foo
方法之后打印,
@Component
@Aspect
public class SystemArchitecture {
@After("execution(* foo(..))")
public void after() {
System.out.println("@After");
}
}
而A fooserviceimpl
用实现的foo
方法
@Service
public class FooServiceImpl implements FooService{
@Override
public FooDto foo(String msg) {
return new FooDto(msg);
}
}
代码工作,而我可以看到“@oft”打印到控制台上,但是我无法使用以下测试来调用 pointcut之后的。
@SpringBootTest
public class AspectTest {
@Autowired
private FooService fooService;
@Test
void shouldPass() {
fooService.foo("hello");
}
}
我还尝试过使用非比恩代理,正如 https://stackover.com/a/a/a/a/56312984/182224588 ,但是这次我得到一个明显的错误无法扩展具体方面
,因为我的间谍代理不再被视为一个方面:
public class AspectNoContextTest {
@Test
void shouldPass() {
FooService fooService = Mockito.mock(FooService.class);
SystemArchitecture systemArchitecture = Mockito.spy(new SystemArchitecture());
AspectJProxyFactory aspectJProxyFactory = new AspectJProxyFactory(fooService);
aspectJProxyFactory.addAspect(systemArchitecture);
DefaultAopProxyFactory proxyFactory = new DefaultAopProxyFactory();
AopProxy aopProxy = proxyFactory.createAopProxy(aspectJProxyFactory);
FooService proxy = (FooService) aopProxy.getProxy();
proxy.foo("foo");
verify(systemArchitecture, times(1)).after();
}
}
I have a dummy project where I try figure out how to test pointcuts being triggered.
My project consists of 1 aspect bean which just prints after a foo
method is called
@Component
@Aspect
public class SystemArchitecture {
@After("execution(* foo(..))")
public void after() {
System.out.println("@After");
}
}
And a FooServiceImpl
with implemented foo
method
@Service
public class FooServiceImpl implements FooService{
@Override
public FooDto foo(String msg) {
return new FooDto(msg);
}
}
The code works and and I can see "@After" being printed to console, but I can't check programatically if after
pointcut was called using the test below.
@SpringBootTest
public class AspectTest {
@Autowired
private FooService fooService;
@Test
void shouldPass() {
fooService.foo("hello");
}
}
I've also tried using non-bean proxy as was adviced in https://stackoverflow.com/a/56312984/18224588, but this time I'm getting an obvious error cannot extend concrete aspect
because my spy proxy is no longer viewed as an aspect:
public class AspectNoContextTest {
@Test
void shouldPass() {
FooService fooService = Mockito.mock(FooService.class);
SystemArchitecture systemArchitecture = Mockito.spy(new SystemArchitecture());
AspectJProxyFactory aspectJProxyFactory = new AspectJProxyFactory(fooService);
aspectJProxyFactory.addAspect(systemArchitecture);
DefaultAopProxyFactory proxyFactory = new DefaultAopProxyFactory();
AopProxy aopProxy = proxyFactory.createAopProxy(aspectJProxyFactory);
FooService proxy = (FooService) aopProxy.getProxy();
proxy.foo("foo");
verify(systemArchitecture, times(1)).after();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,经过一番挖掘,我发现可以通过使方面
@spybean
来实现这一目标。aoputils
可用于执行其他检查Ok, after some digging, I found that it's possible to accomplish this by making an aspect a
@SpyBean
. AlsoAopUtils
can be used for performing additional checks