在单元测试中使用 AspectJ 用模拟替换第三方对象
我正在使用 Spring-WS 和 WebServiceTemplate 类编写一个 Web 服务客户端。在 WebServiceTemplate 类的碗中,创建了一个 WebServiceConnection。 WebServiceConnection.send 用于实际发送消息。我想要做的是拦截对 WebServiceConnection.send 的调用,并将其替换为一些检查传递给 WebServiceConnetion.send 的对象的逻辑。
我突然意识到这将是使用方面的好地方。但是,我不确定如何仅在执行单元测试时才运行这些方面。我还希望根据我正在执行的测试运行不同的方面。
有人对如何做到这一点有任何想法吗?
I'm writing a web services client using Spring-WS and the WebServiceTemplate class. Down in the bowls of the WebServiceTemplate class, a WebServiceConnection is created. WebServiceConnection.send is used to actually send the message. What I'd like to do is intercept the call to WebServiceConnection.send and replace it with some logic that examines the object passed to WebServiceConnetion.send.
It strikes me that this would be a good place to use Aspects. However, I'm not sure how I can have the aspects run only when I'm executing the unit tests. I would also like to have a different aspects run based on what tests I'm executing.
Anyone have any ideas on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将运行时编织与 AspectJ 结合使用。您不必将这些方面编译到您的应用程序中,在测试时包含它们就足够了。
由于使用 AspectJ 时类路径上必须有 META-INF/aop.xml,并且您必须使用 -agent:myPath/aspectjweaver.jar 启动 JVM,因此您手头有工具,仅在测试时才使用 AspectJ 。
哦,如果您使用 AspectJ 编译您的应用程序,如果您结合运行时编织和编译时编织,您仍然可以在测试时使用其他方面。
You can use Runtime Weaving with AspectJ. You don't have to compile the aspects into yout app, it is enought to include them when testing.
Since there has to be a META-INF/aop.xml on the classpath when using AspectJ, and since you have to start the JVM with -agent:myPath/aspectjweaver.jar, you have your tools at hand to use AspectJ only when testing.
Oh, and if you use AspectJ to compile your app, you can still use additional aspects when testing if you combine runtime weaving and compile time weaving.