开发者

Using AspectJ to replace third party objects with mocks in Unit Tests

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 sen开发者_运维百科d 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜